当前位置:网站首页>C++在C的基础上改进了哪些细节
C++在C的基础上改进了哪些细节
2020-11-08 07:14:00 【osc_ix000whh】
C++ 是在C语言的基础上改进的,C语言的很多语法在 C++ 中依然广泛使用,例如:
C++ 仍然使用 char、short、int、long、float、double 等基本数据类型;
C++ 仍然使用 if...else、while、for、switch、break 等分支或循环结构;
C++ 仍然使用 +、-、*、/、%、++、--、<<、>> 等运算符;
C++ 仍然使用 typedef、#define、enum、struct 等;
C++ 仍然使用C语言中经典的指针(Pointer),并且使用范围有增无减,甚至不可或缺。
下面我们再来谈一下C++改进了哪些细节。
变量定义位置
ANSI C 规定,所有局部变量都必须定义在函数开头,在定义好变量之前不能有其他的执行语句。C99 标准取消这这条限制,但是 VC/VS 对 C99 的支持很不积极,仍然要求变量定义在函数开头。请看下面的代码:
· #include <stdio.h>
· · int main(){
· · int a;
· · scanf("%d", &a);
· · int b;
· · scanf("%d", &b);
· · int c = a + b;
· · printf("%d\n", c);
· ·
· · return 0;
· · }
· 将代码保存到源文件main.c,那么它可以在 GCC、Xcode 下编译通过,但在 VC/VS 下会报错。GCC、Xcode 对 C99 的支持非常好,可以在函数的任意位置定义变量;但 VC/VS 对 C99 的支持寥寥无几,必须在函数开头定义好所有变量。
将上面的代码再保存到源文件main.cpp,那么它在 GCC、Xcode、VC/VS 下都可以编译通过。这是因为 C++ 取消了原来的限制,变量只要在使用之前定义好即可,不强制必须在函数开头定义所有变量。
注意源文件的后缀,.c是C语言代码,.cpp是C++代码,它们的编译方式不同。
取消限制带来的另外一个好处是,可以在 for 循环的控制语句中定义变量,请看下面的例子:
· #include <iostream>
· · using namespace std;
· ·
· · int sum(int n){
· · int total = 0;
· · //在for循环的条件语句内部定义变量i
· · for(int i=1; i<=n ;i++){
· · total += i;
· · }
· · return total;
· · }
· ·
· · int main(){
· · cout<<"Input a interge: ";
· · int n;
· · cin>>n;
· · cout<<"Total: "<<sum(n)<<endl;
· · return 0;
· · }
· 运行结果:
Input a interge: 10
Total: 55
在 for 内部定义循环控制变量 i,会让代码看起来更加紧凑,并使得 i 的作用域被限制在整个 for 循环语句内部(包括循环条件和循环体),减小了命名冲突的概率。在以后的编码过程中,我推荐这种写法。
在学习的过程中总会遇见一些困难与疑惑,我也是这样过来的,知道你们的难处。所以我创建了一个企鹅群:105+302+98+69,用来交流学习的,有不懂的问题可以在群里提问,群里也会有我之前学习C/C++的学习资料跟我之前VIP上课的课程视频,都可以免费分享给大家,让我们一起成长,成为一个优秀的人。
布尔类型(bool)
在C语言中,关系运算和逻辑运算的结果有两种,真和假:0 表示假,非 0 表示真。例如:
· #include <stdio.h>
· · int main(){
· · int a, b, flag;
· · scanf("%d %d", &a, &b);
· · flag = a > b; //flag保存关系运算结果
· · printf("flag = %d\n", flag);
· ·
· · return 0;
· · }
· 运行结果:
10 20
flag = 0
C语言并没有彻底从语法上支持“真”和“假”,只是用 1 和 0 来代表。这点在 C++ 中得到了改善,C++ 新增了bool类型,它一般占用 1 个字节长度。bool 类型只有两个取值,true 和 false:true 表示“真”,false 表示“假”。请看下面的例子:
· #include <iostream>
· · using namespace std;
· ·
· · int main(){
· · int a, b;
· · bool flag; //布尔变量
· · cin>>a>>b;
· · flag = a > b;
· · cout<<"flag = "<<flag<<endl;
· ·
· · return 0;
· · }
· 10 20
flag = 0
遗憾的是,在 C++ 中使用 cout 输出 bool 变量的值时还是用数字 1 和 0 表示,而不是 true 或 false。Java、PHP、JavaScript 等也都支持布尔类型,但输出结果为 true 或 false,我武断地认为这样更科学。
你也可以显式地对 bool 变量赋值,例如:
· #include <iostream>
· · using namespace std;
· ·
· · int main(){
· · bool flag = true;
· · if(flag){
· · cout<<"true"<<endl;
· · }else{
· · cout<<"false"<<endl;
· · }
· ·
· · flag = false;
· · if(flag){
· · cout<<"true"<<endl;
· · }else{
· · cout<<"false"<<endl;
· · }
· ·
· · return 0;
· · }
· 运行结果:
true
false
注意,true 和 false 是 C++ 中的关键字。
在以后的编码中,我推荐使用 bool 变量来表示逻辑运算、关系运算以及开关变量的值
版权声明
本文为[osc_ix000whh]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4302478/blog/4707814
边栏推荐
- Littlest jupyterhub| 02 using nbgitpuller to distribute shared files
- C/C++编程笔记:C语言相比其他编程语言,有什么不一样的优势?
- Golang anonymous structure member, named structure member, inheritance, composition
- 尾-递
- golang 匿名结构体成员,具名结构体成员,继承,组合
- Unparseable date: 'mon Aug 15 11:24:39 CST 2016', time format conversion exception
- PerconaXtraDBCluster8.0 最详尽用法指南
- CPP (2) creating CPP project
- ulab 1.0.0发布
- Basic knowledge of C + +
猜你喜欢
swiper 窗口宽度变化,页面宽度高度变化 导致自动滑动 解决方案
use Xunit.DependencyInjection Transformation test project
What kind of technical ability should a programmer who has worked for 1-3 years? How to improve?
Go之发送钉钉和邮箱
Improvement of maintenance mode of laravel8 update
[solution] distributed timing task solution
鼠标变小手
QT hybrid Python development technology: Python introduction, hybrid process and demo
iOS 学习笔记二【cocopods安装使用和安装过程中遇到的问题及解决办法】【20160725更新】
Sum up some useful functions
随机推荐
scala 中 Future 的简单使用
2020-11-07:已知一个正整数数组,两个数相加等于N并且一定存在,如何找到两个数相乘最小的两个数?
京淘项目知识点总结
Speed up your website with jsdelivr
The instanceof operator in ecmascript7 specification
Wanxin Finance
CPP (1) installation of cmake
鼠标变小手
Everything is 2020, LINQ query you are still using expression tree
Problems of Android 9.0/p WebView multi process usage
Data transmission of asynchronous serial communication controlled by group bus communication
Sentry installation
0.计算机简史
PerconaXtraDBCluster8.0 最详尽用法指南
Improvement of maintenance mode of laravel8 update
Windows subsystem Ubuntu installation
Lay UI left tree Dtree right list table
Adobe Lightroom / LR 2021 software installation package (with installation tutorial)
Wechat nickname Emoji expression, special expression causes the list not to be displayed, export excel error report and other problems solved!
On the concurrency of update operation