当前位置:网站首页>What details does C + + improve on the basis of C
What details does C + + improve on the basis of C
2020-11-09 08:25:00 【osc_anefoz50】
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_anefoz50]所创,转载请带上原文链接,感谢
边栏推荐
- Talk about my understanding of FAAS with Alibaba cloud FC
- Five design patterns frequently used in development
- C / C + + Programming Notes: pointer! Understand pointer from memory, let you understand pointer completely
- AQS 都看完了,Condition 原理可不能少!
- Operation 2020.11.7-8
- 首次开通csdn,这篇文章送给过去的自己和正在发生的你
- [Python从零到壹] 五.网络爬虫之BeautifulSoup基础语法万字详解
- LTM understanding and configuration notes
- For the first time open CSDN, this article is for the past self and what is happening to you
- 【QT】子类化QThread实现多线程
猜你喜欢

C + + adjacency matrix

常见特征金字塔网络FPN及变体

Application of cloud gateway equipment on easynts in Xueliang project

失业日志 11月5日

How does semaphore, a thread synchronization tool that uses an up counter, look like?

14. Introduction to kubenetes

Combine theory with practice to understand CORS thoroughly

上线1周,B.Protocal已有7000ETH资产!

Teacher Liang's small class

Finally, the python project is released as exe executable program process
随机推荐
C++邻接矩阵
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!
WordPress Import 上传的文件尺寸超过php.ini中定义的upload_max_filesize值--&gt;解决方法。
range_sensor_layer
Share API on the web
C / C + + Programming Notes: pointer! Understand pointer from memory, let you understand pointer completely
Core knowledge of C + + 11-17 template (2) -- class template
In 2020, what are the best tools for Android developers to break the cold winter?
一堆代码忘了缩进?快捷方式教你无忧无虑!
Android emulator error: x86 emulation currently requires hardware acceleration solution
Several common playing methods of sub database and sub table and how to solve the problem of cross database query
1.操作系统是干什么的?
Linked blocking queue based on linked list
Get the first cover image of video through canvas
B. protocal has 7000eth assets in one week!
Common feature pyramid network FPN and its variants
2020,Android开发者打破寒冬的利器是什么?
基于链表的有界阻塞队列 —— LinkedBlockingQueue
When iperf is installed under centos7, the solution of make: * no targets specified and no makefile found. Stop
几行代码轻松实现跨系统传递 traceId,再也不用担心对不上日志了!