c和c++中const的区别

 

c和c++中的const从语义上讲没什么太大的区别,但毕竟两种语言设计思想还有很大区别,在实现的时候不尽相同。

请编译test.c 和test.cpp,看一点区别。

 

 

 

test.c

 

#include<stdio.h> void main() { //网上认为这个会编译出错,但是却没有. //申请数组时,这个与编译器有关,请关注c99特性。 printf("I am c/n"); const int n = 10; int array[i]; //此处与c++不同 const int a = 10; int * pa = &a; *pa = 20; printf("%d/n",*pa); } 

 

test.cpp

 

#include<iostream> using namespace std; int main() { //c++中允许申请大小为一个变量的数组 cout<<"I am c++"<<endl; const int n=10; int array[n]; //编译出错 const int a = 10; int * pa = &a; *pa = 20; return 0; }  

 

 

你可能感兴趣的:(c和c++中const的区别)