当前位置:网站首页>What details does C + + improve on the basis of C
What details does C + + improve on the basis of C
2020-11-08 07:14:00 【osc_ix000whh】
C++ Is in C Based on the improvement of language ,C A lot of grammar of language is in C++ Is still widely used , for example :
C++ Still use char、short、int、long、float、double Basic data type ;
C++ Still use if...else、while、for、switch、break As a branching or cyclic structure ;
C++ Still use +、-、*、/、%、++、--、<<、>> Equal operator ;
C++ Still use typedef、#define、enum、struct etc. ;
C++ Still use C The classic pointer in language (Pointer), And the scope of use has increased , Even indispensable .
Now let's talk about C++ What details have been improved .
Variable definition position
ANSI C Regulations , All local variables must be defined at the beginning of the function , There must be no other execution statement until the variable is defined .C99 The standard removes this restriction , however VC/VS Yes C99 It's not very positive , It still requires variables to be defined at the beginning of the function . Look at the code below :
· #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;
· · }
· Save the code to the source file main.c, So it can be in GCC、Xcode Compiled through , But in VC/VS I'll make a mistake next time .GCC、Xcode Yes C99 Our support is very good , You can define variables anywhere in a function ; but VC/VS Yes C99 There is little support for , All variables must be defined at the beginning of the function .
Save the above code to the source file again main.cpp, So it's in GCC、Xcode、VC/VS Can be compiled under . This is because C++ The original restrictions have been lifted , Variables are defined before they are used , It is not mandatory that all variables must be defined at the beginning of a function .
Note the suffix of the source file ,.c yes C The language code ,.cpp yes C++ Code , They compile differently .
Another benefit of lifting restrictions is , Can be in for A variable is defined in the control statement of the loop , Please see the following example :
· #include <iostream>
· · using namespace std;
· ·
· · int sum(int n){
· · int total = 0;
· · // stay for The conditional statement of the loop defines variables inside 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;
· · }
· Running results :
Input a interge: 10
Total: 55
stay for Internally defined loop control variables i, It makes the code look more compact , And make i The scope of is limited to the whole of for Inside the loop statement ( Including cycle conditions and loop bodies ), It reduces the probability of naming conflicts . In the subsequent coding process , I recommend this way of writing .
In the process of learning, there will always be some difficulties and doubts , I came here the same way , Know your difficulties . So I created a colony of penguins :105+302+98+69, For communication and learning , If you have questions you don't understand, you can ask them in the group , There will also be me in the group C/C++ The study materials of the students are the same as before VIP Class video , Can be shared with you for free , Let's grow together , To be an excellent person .
Boolean type (bool)
stay C In language , There are two results of relational operation and logical operation , True and false :0 Said the false , Not 0 Said really . for example :
· #include <stdio.h>
· · int main(){
· · int a, b, flag;
· · scanf("%d %d", &a, &b);
· · flag = a > b; //flag Save the result of relational operations
· · printf("flag = %d\n", flag);
· ·
· · return 0;
· · }
· Running results :
10 20
flag = 0
C Language doesn't support grammar completely “ really ” and “ false ”, Just use 1 and 0 To represent the . This is in C++ Has been improved in ,C++ Added bool type , It usually occupies 1 Byte length .bool Type has only two values ,true and false:true Express “ really ”,false Express “ false ”. Please see the following example :
· #include <iostream>
· · using namespace std;
· ·
· · int main(){
· · int a, b;
· · bool flag; // Boolean variables
· · cin>>a>>b;
· · flag = a > b;
· · cout<<"flag = "<<flag<<endl;
· ·
· · return 0;
· · }
· 10 20
flag = 0
Unfortunately , stay C++ Use in cout Output bool The value of a variable is still a number 1 and 0 Express , instead of true or false.Java、PHP、JavaScript They also support Boolean types , But the output is true or false, I arbitrarily think it's more scientific .
You can also be explicit about bool Variable assignment , for example :
· #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;
· · }
· Running results :
true
false
Be careful ,true and false yes C++ Keywords in .
In later coding , I recommend using bool Variables are used to represent logical operations 、 Relational operations and the values of switching variables
版权声明
本文为[osc_ix000whh]所创,转载请带上原文链接,感谢
边栏推荐
- UCGUI简介
- Macquarie Bank drives digital transformation with datastex enterprise (DSE)
- C++基础知识篇:C++ 基本语法
- laravel8更新之速率限制改进
- GET,POST,PUT,DELETE,OPTIONS用法与说明
- Astra: Apache Cassandra的未来是云原生
- CPP (3) what is cmake
- iOS 学习笔记二【cocopods安装使用和安装过程中遇到的问题及解决办法】【20160725更新】
- Android Basics - RadioButton (radio button)
- CPP (1) installation of cmake
猜你喜欢
NOIP 2012 提高组 复赛 第一天 第二题 国王游戏 game 数学推导 AC代码(高精度 低精度 乘 除 比较)+60代码(long long)+20分代码(全排列+深搜dfs)
Qt混合Python开发技术:Python介绍、混合过程和Demo
Solve the problem of rabbitmq message loss and repeated consumption
swiper 窗口宽度变化,页面宽度高度变化 导致自动滑动 解决方案
Visual studio 2015 unresponsive / stopped working problem resolution
Wanxin Finance
Idea - the. IML file was not automatically generated by the project
解决RabbitMQ消息丢失与重复消费问题
QT hybrid Python development technology: Python introduction, hybrid process and demo
Ulab 1.0.0 release
随机推荐
Wechat applet request reported 400 error @ requestbody failed to receive
Abnormal + Abstract
双向LSTM在时间序列异常值检测的应用
Sentry installation
nvm
SQL Server 2008R2 18456 error resolution
接口
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?
Android Basics - RadioButton (radio button)
FORTRAN 77 reads some data from the file and uses the heron iteration formula to solve the problem
Learn Scala if Else statement
swiper 窗口宽度变化,页面宽度高度变化 导致自动滑动 解决方案
Writing method of field and field comparison condition in where condition in thinkphpp6
Solve the problem of rabbitmq message loss and repeated consumption
Jingtao project day09
CPP (2) creating CPP project
The software in your host has terminated an established connection. resolvent
Judging whether paths intersect or not by leetcode
Do you really understand the high concurrency?
The most detailed usage guide for perconaxtradbcluster8.0