Type of #define variables

后端 未结 7 2192
一个人的身影
一个人的身影 2020-11-27 17:33

If I have:

#define MAXLINE    5000

What type is MAXLINE understood to be? Should I assume it is an int? Can I test it somehow?

7条回答
  •  粉色の甜心
    2020-11-27 18:16

    Yes, you can assume it's an int.

    Well, actually all the other answers are correct. It's not C, it's just a directive that tells the preprocessor to do some textual substitutions, and as such it has no type. However, if you do not do any funky things with it (like the ## preprocessor trick), you will typically use MAXLINE like some kind of constant, and the preprocessor will replace it with 5000 which is indeed an explicit constant. And constants do have type: 5000 is an int. A constant written as a decimal integer, with no suffix (like U or L), will be interpreted by the compiler as an int, long int or unsigned long int: the first of these types that fits.

    But this has of course nothing to do with the preprecessor. You could rewrite your question as “what is the type of 5000?”, with no #define.

提交回复
热议问题