const in C and C++

The const keyword has “constant” and “read-only variable” semantics in C++, but only “read-only variable” semantics in C.

To illustrate this difference, try compiling the following code:

const int x = 2;
int y[x];

This is not legal C, because x is not a compile-time constant semantically.

But it is legal C++, because const also has constant semantics and thus x is a compile-time constant.


Related posts:

  1. The Uncertainty Principle
  2. NPTL_KERN_VER
  3. What exactly “-march=native” means
  4. Rvalue reference

Tags:

Leave a Reply

Hint: Register at Gravatar and your comments will be accompanied by your personalized icon.