当前位置:网站首页>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
边栏推荐
- Privacy violation and null dereference of fortify vulnerability
- iOS上传App Store报错:this action cannot be completed -22421 解决方案
- Mouse small hand
- [original] about the abnormal situation of high version poi autosizecolumn method
- nvm
- CPP (4) boost installation and basic use for Mac
- 接口
- FORTRAN77从文件中读入若干数据并用heron迭代公式开方
- 到底选openstack还是vmware?
- Basic knowledge of C + +
猜你喜欢
WPF personal summary on drawing
Face recognition: attack types and anti spoofing techniques
PerconaXtraDBCluster8.0 最详尽用法指南
0.计算机简史
The emergence and significance of micro service
鼠标变小手
Improvement of maintenance mode of laravel8 update
ts流中的pcr与pts计算与逆运算
Wechat applet request reported 400 error @ requestbody failed to receive
分布式共识机制
随机推荐
Visual Studio 2015 未响应/已停止工作的问题解决
WPF personal summary on drawing
Assembly function MCALL systemstack asmcgocal system call
Introduction to ucgui
Insight -- the application of sanet in arbitrary style transfer
尾-递
Basic knowledge of C + +
QT hybrid Python development technology: Python introduction, hybrid process and demo
分布式共识机制
C++在C的基础上改进了哪些细节
Goland 编写含有template的程序
China Telecom announces 5g SA commercial scale in 2020
Adobe Prelude / PL 2020 software installation package (with installation tutorial)
GoLand writes a program with template
Idea - the. IML file was not automatically generated by the project
GET,POST,PUT,DELETE,OPTIONS用法与说明
C语言I博客作业03
Visual studio 2015 unresponsive / stopped working problem resolution
微信昵称emoji表情,特殊表情导致列表不显示,导出EXCEL报错等问题解决!
学习Scala IF…ELSE 语句