Why does #define not require a semicolon?

前端 未结 7 990
无人及你
无人及你 2020-11-27 21:03

I was writing some test code in C. By mistake I had inserted a ; after a #define, which gave me errors. Why is a semicolon not req

7条回答
  •  暖寄归人
    2020-11-27 21:31

    The second version does not define a constant as far as the language is concerned, just a substitution rule for a block of text. Once the preprocessor has done it's job, the compiler sees

    char buffer [256;];

    which is not syntactically valid.

    The moral of the story: prefer the const int MAX_STRING = 256; way as that helps you, the compiler, and the debugger.

提交回复
热议问题