当前位置:网站首页>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
边栏推荐
- OSChina 周日乱弹 —— 之前呢,我一直以为自己是个……
- On the stock trading of leetcode
- 异常+abstract
- Astra: the future of Apache Cassandra is cloud native
- Improvement of maintenance mode of laravel8 update
- Writing method of field and field comparison condition in where condition in thinkphpp6
- Summary of knowledge points of Jingtao project
- IOS upload app store error: this action cannot be completed - 22421 solution
- FORTRAN77从文件中读入若干数据并用heron迭代公式开方
- Adobe Lightroom / LR 2021 software installation package (with installation tutorial)
猜你喜欢
Judging whether paths intersect or not by leetcode
Go之发送钉钉和邮箱
Wechat nickname Emoji expression, special expression causes the list not to be displayed, export excel error report and other problems solved!
在Ubuntu上体验最新版本EROFS
C language I blog assignment 03
More than 50 object detection datasets from different industries
Privacy violation and null dereference of fortify vulnerability
团灭 LeetCode 股票买卖问题
Simple use of future in Scala
Download, installation and configuration of Sogou input method in Ubuntu
随机推荐
Awk implements SQL like join operation
Android 9.0/P WebView 多进程使用的问题
The real-time display of CPU and memory utilization rate by Ubuntu
A compilation bug brought by vs2015 Update1 update [existing solutions]
PerconaXtraDBCluster8.0 最详尽用法指南
Oschina plays on Sunday - before that, I always thought I was a
Astra: Apache Cassandra的未来是云原生
Web Security (1) -- browser homology strategy
高并发,你真的理解透彻了吗?
Sum up some useful functions
November 07, 2020: given an array of positive integers, the sum of two numbers equals N and must exist. How to find the two numbers with the smallest multiplication?
Qt混合Python开发技术:Python介绍、混合过程和Demo
QT hybrid Python development technology: Python introduction, hybrid process and demo
麦格理银行借助DataStax Enterprise (DSE) 驱动数字化转型
C / C + + Programming Notes: what are the advantages of C compared with other programming languages?
京淘项目知识点总结
C language I blog assignment 03
归纳一些比较好用的函数
Distributed consensus mechanism
状态压缩:对动态规划进行降维打击