Example of something which is, and is not, a “Constant Expression” in C?

后端 未结 6 1414
醉梦人生
醉梦人生 2020-12-09 11:54

I\'m a tad confused between what is and is not a Constant Expression in C, even after much Googleing. Could you provide an example of something which is, and which is not, a

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-09 12:51

    Another fun little wrinkle: in C, the value of an 'enum' is a constant, but may only be used after the declaration of the 'enum' is complete. The following, for example, is not acceptable in standard C, though it is acceptable in C++:

    enum {foo=19, bar, boz=bar+5;};
    

    It could be rewritten:

    enum {foo=19, bar}; enum {boz=bar+5;};
    

    though this would end up defining multiple different enumeration types, rather than one which holds all the values.

提交回复
热议问题