CPP与C
给算法竞赛入门阶段的同学的快速c转c++速效救心丸。不适合系统学习使用。
区别1:头文件不同
C++头文件没有.h
1 2 3 4 5 6
| C语言: #include <stdio.h>
C++语言: #include <cstdio> #include <stack>
|
如果要在c++代码中引用c语言的库,去掉.h,并在最前面加上c.
1 2 3 4
| string.h >>> cstring stdio.h >>> cstdio stdlib.h >>> cstdlib ...
|
区别2:读入输出方式不同.
c++语言中只要引用了库就仍然可以用c语言的输入输出函数.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| C++: #include <iostream> using namespace std; int main(){ int a; char b; char str[100] = {0}; cin>>a>>b>>str; cout<<a<<b<<str<<endl; }
|
区别3:命名空间
死记硬背即可, c++代码需要在引用头文件后添加一行
非竞赛代码不推荐使用,会污染命名空间。
区别4:布尔类型
c++语言自带布尔类型.存储的是真和假两种情况.
1 2 3 4 5 6 7 8
| bool a = false; a = true;
if(a){ foo(); }else{ bar(); }
|
c语言引用stdbool.h后也有,但是课内不讲