C vs C++ – How Does ‘const’ Differ?

c++constants

How does the const qualification on variables differ in C and C++?

from: Does "const" just mean read-only or something more?

"What prompted this question was this answer: https://stackoverflow.com/questions/4024318#4024417 where he states const "just" means read-only in C. I thought that's all const meant, regardless of whether it was C or C++. What does he mean?"

Best Answer

const in C cannot be used to build constant expressions.

For example :

#include <stdio.h>
int main()
{
   int i = 2;
   const int C = 2;
   switch(i)
   {
      case C  : printf("Hello") ;
      break;

      default : printf("World");
   }
}

doesn't work in C because case label does not reduce to an integer constant.