当前位置:网站首页>C++在C的基础上改进了哪些细节
C++在C的基础上改进了哪些细节
2020-11-09 08:25:00 【osc_anefoz50】
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_anefoz50]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4287145/blog/4708532
边栏推荐
- App crashed inexplicably. At first, it thought it was the case of the name in the header. Finally, it was found that it was the fault of the container!
- Service grid is still difficult - CNCF
- 5 个我不可或缺的开源工具
- 2020,Android开发者打破寒冬的利器是什么?
- Oschina plays disorderly on Monday
- How to reduce the resource consumption of istio agent through sidecar custom resource
- SaaS: another manifestation of platform commercialization capability
- FC 游戏机的工作原理是怎样的?
- 商品管理系统——整合仓库服务以及获取仓库列表
- OpenGL ES 框架详细解析(八) —— OpenGL ES 设计指南
猜你喜欢

卧槽,这年轻人不讲武德,应届生凭“小抄”干掉5年老鸟,成功拿到字节20Koffer

AQS 都看完了,Condition 原理可不能少!

How to reduce the resource consumption of istio agent through sidecar custom resource

Windows环境下如何进行线程Dump分析

B. protocal has 7000eth assets in one week!

Android emulator error: x86 emulation currently requires hardware acceleration的解决方案

第五章编程

LTM understanding and configuration notes

20201108编程练习——练习3

14.Kubenetes简介
随机推荐
当我们聊数据质量的时候,我们在聊些什么?
Installation record of SAP s / 4hana 2020
你有没有想过为什么交易和退款要拆开不同的表
A few lines of code can easily transfer traceid across systems, so you don't have to worry about losing the log!
一堆代码忘了缩进?快捷方式教你无忧无虑!
华为HCIA笔记
自然语言处理(NLP)路线图 - kdnuggets
How does semaphore, a thread synchronization tool that uses an up counter, look like?
Commodity management system -- integrate warehouse services and obtain warehouse list
Chapter 5 programming
平台商业化能力的另一种表现形式SAAS
Concurrent linked queue: a non blocking unbounded thread safe queue
常见特征金字塔网络FPN及变体
Ten year itch of programmer
架构中台图
WordPress Import 上传的文件尺寸超过php.ini中定义的upload_max_filesize值--&gt;解决方法。
LTM understanding and configuration notes
Depth first search and breadth first search
图节点分类与消息传递 - 知乎
Leetcode-11: container with the most water