When any expressio n uses a constant non-volatile variable, the Compiler replaces it by the constant value the variable holds. This requires less code than taking the object itself.
If no expression takes the address of the constant non-volatile object, the Compiler removes the object itself (notice ci in the following listing). This uses less memory space.
void f(void) { const int ci = 100; // ci removed (no address taken) const int ci2 = 200; // ci2 not removed (address taken below) const volatile int ci3 = 300; // ci3 not removed (volatile) int i; int *p; i = ci; // replaced by i = 100; i = ci2; // no replacement p = &ci2; // address taken }
The Compiler does not remove global constant non-volatile variables. They are replaced in expressions by the constant value they hold.
The Compiler optimizes constant non-volatile arrays (notice array[] in the following listing).
void g(void) { const int array[] = {1,2,3,4}; int i; i = array[2]; // replaced by i=3; }