当前位置:网站首页>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
边栏推荐
- A few lines of code can easily transfer traceid across systems, so you don't have to worry about losing the log!
- Graph node classification and message passing
- 作业2020.11.7-8
- 2.计算机硬件简介
- A bunch of code forgot to indent? Shortcut teach you carefree!
- Depth first search and breadth first search
- 结合阿里云 FC 谈谈我对 FaaS 的理解
- How does pipedrive support quality publishing with 50 + deployments per day?
- Android emulator error: x86 emulation currently requires hardware acceleration的解决方案
- Commodity management system -- the search function of SPU
猜你喜欢

Adding OpenGL form to MFC dialog

Detailed analysis of OpenGL es framework (8) -- OpenGL es Design Guide

基于LabVIEW实现的几种滚动字幕

3.你知道计算机是如何启动的吗?

Teacher Liang's small class

Factory pattern pattern pattern (simple factory, factory method, abstract factory pattern)

结合阿里云 FC 谈谈我对 FaaS 的理解

Bifrost 之 文件队列(一)

Sublime text3 插件ColorPicker(调色板)不能使用快捷键的解决方法

A brief introduction of C code to open or close the firewall example
随机推荐
首次开通csdn,这篇文章送给过去的自己和正在发生的你
C + + adjacency matrix
几行代码轻松实现跨系统传递 traceId,再也不用担心对不上日志了!
Service grid is still difficult - CNCF
为什么我们不使用GraphQL? - Wundergraph
Exception capture and handling in C + +
2 normal mode
The vowels in the inverted string of leetcode
商品管理系统——整合仓库服务以及获取仓库列表
Factory pattern pattern pattern (simple factory, factory method, abstract factory pattern)
Introduction to nmon
The difference between GDI and OpenGL
Linked blocking queue based on linked list
A few lines of code can easily transfer traceid across systems, so you don't have to worry about losing the log!
A few lines of code can easily transfer traceid across systems, so you don't have to worry about losing the log!
Five design patterns frequently used in development
Share API on the web
Linked list
六家公司CTO讲述曾经历的“宕机噩梦”
C++之异常捕获和处理