Size of #define values

前端 未结 5 1418
野趣味
野趣味 2021-02-14 04:18

If a value is defined as

#define M_40 40

Is the size the same as a short (2 bytes) or is it as a char (1 byte) or

5条回答
  •  隐瞒了意图╮
    2021-02-14 04:52

    #define has no size as it's not a type but a plain text substitution into your C++ code. #define is a preprocessing directive and it runs before your code even begins to be compiled .

    The size in C++ code after substitution is whatever the size is of what C++ expression or code you have there. For example if you suffix with L like 102L then it is seen a long, otherwise with no suffix, just an int. So 4 bytes on x86 and x64 probably, but this is compiler dependent.

    Perhaps the C++ standard's Integer literal section will clear it up for you (Section 2.13.1-2 of the C++03 standard):

    The type of an integer literal depends on its form, value, and suffix. If it is decimal and has no suffix, it has the first of these types in which its value can be represented: int, long int; if the value cannot be represented as a long int, the behavior is undefined. If it is octal or hexadecimal and has no suffix, it has the first of these types in which its value can be represented: int, unsigned int, long int, unsigned long int. If it is suffixed by u or U, its type is the first of these types in which its value can be represented: unsigned int, unsigned long int. If it is suffixed by l or L, its type is the first of these types in which its value can be represented: long int, unsigned long int. If it is suffixed by ul, lu, uL, Lu, Ul, lU, UL, or LU, its type is unsigned long int

提交回复
热议问题