Size of #define values

前端 未结 5 1430
野趣味
野趣味 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

    0 讨论(0)
  • 2021-02-14 05:06

    A plain integer is going to be implicitly cast to int in all calculations and assignments.

    #define simply tells the preprocessor to replace all references to a symbol with something else. This is the same as doing a global find-replace on your code and replacing M_40 with 40.

    0 讨论(0)
  • 2021-02-14 05:10

    A #define value has no size, specifically. It's just text substitution. It depends on the context of where (and what) is being substituted.

    In your example, where you use M_40, the compile will see 40, and usually treat it as in int.

    However, if we had:

    void SomeFunc(long);
    
    SomeFunc(M_40);
    

    It will be treated as a long.

    0 讨论(0)
  • 2021-02-14 05:12

    The preprocessor just does simple text substitution, so the fact that your constant is in a #define doesn't matter. All the C standard says is that "Each constant shall have a type and the value of a constant shall be in the range of representable values for its type." C++ is likely to not vary too much from that.

    0 讨论(0)
  • 2021-02-14 05:13

    Preprocessor macros get literally swapped in during the preprocess stage of the compilation.

    For example the code

    #define N 5
    
    int value = N;
    

    will get swapped for

    int value = 5;
    

    when the compiler sees it. It does not have a size of its own as such

    0 讨论(0)
提交回复
热议问题