Makes variable value or pointer parameter unmodifiable.
When const is used with a variable, it uses the following syntax:
const variable-name [ = value];
In this case, the const modifier allows you to assign an initial value to a variable that cannot later be changed by the program. For example,
const my_age = 32;
Any assignments to 'my_age' will result in a compiler error. However, such declaration is quite different than using
#define my_age 32
In the first case, the compiler allocates a memory for 'my_age' and stores the initial value 32 there, but it will not allow any later assignment to this variable. But, in the second case, all occurences of 'my_age' are simply replaced with 32 by the preprocessor, and no memory will be allocated for it.
Partager