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. Rvalue reference
  3. Acquire and Release Semantics

Tags:

Leave a Reply

*

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