c代码中可以声明三个相同的全局变量不会报错,c会将其与两个作为外部声明,等效于前面加了extern
c++中会直接报错
int a;
int a;
int a;
变量类型
c中函数的形参可以不写数据类型,默认会被当成int型
c++中会报错
void test(a,b){
}
类型转换
c代码中malloc开辟内存会返回一个void类型的指针,可以将指针直接赋值给char。
在c++中会报错,必须强制类型转换
char* c;
c = malloc(100);
struct
c代码中结构体定义需要写struct
c++中可以省略
struct student{
char* name;
}
int main(){
struct student stu;//c
student stu;//c++
}
bool
c中bool类型需要头文件
c++中可以直接使用
bool b=true;
三目运算符
c++中三目运算符可以直接当做左值使用
c不可以
int a=10;
int b =20;
(a
c代码中const修饰的局部变量会放在栈区
如果用指针指向这个空间,是可以将内容修改的
c++则不会,c++中如果用指针指向常量,编译器会产生一个临时变量,将临时变量的地址赋给指针。常量本身不会改变。
test(){
const int a=10;
int *p = &a;
*p = 100;
}
在其他c文件中的const修饰的常量,在c代码中可以直接使用extern将该常量拿来用,
在c++中则不行,需要更改为外部链接属性
c:
文件1
const int a=100;
文件2
extern const int a;
printf("%d",a);
c++:
文件1
extern const int a=100;
文件2
extern const int a;
cout << a << endl;