(Do not know why the title write C + + always show, should be csdn new Markdown editor Bug) C + + const and C # # can be used to define constants, the two are different, const is a constant with data type, and the macro constant is not, The compiler can perform static type security checks on the former, which is only character substitution and no type safety check.
The C language of the Const and C + + is also very different, in C language with the const modified variable is still a variable, indicating that the variable is read-only, non-display changes, and in C + + after the const modification, it becomes a constant. For example, the following code:
constint n=10;int a[n];
These two lines of code will be an error in the C language, because the array length must be constant when declaring an array, and the N in this is a variable in the C language. With the const modifier in C + +, N is already equivalent to a constant, so it can be passed.
Then look at the following code:
constint a=3;int* p=&a;*p=4;
This is not allowed in C + + because a const modifier has become a constant, so it is not allowed to be modified, either by explicitly changing the value of a or by modifying its value by other means.
C-Language const and Cplusplus const