How does “const” differ in C and C++?

后端 未结 2 1733
时光说笑
时光说笑 2020-12-14 03:44

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 q

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-14 04:09

    const in C cannot be used to build constant expressions.

    For example :

    #include 
    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.

提交回复
热议问题