C++ #define 值的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3197913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Size of #define values
提问by Brian R. Bondy
If a value is defined as
如果一个值被定义为
#define M_40 40
#define M_40 40
Is the size the same as a short
(2 bytes) or is it as a char
(1 byte) or int
(4 bytes)?
大小与a short
(2字节)相同还是与a char
(1字节)或int
(4字节)相同?
Is the size dependent on whether you are 32-bit or 64-bit?
大小取决于您是 32 位还是 64 位?
回答by Brian R. Bondy
#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 .
#define
没有大小,因为它不是一种类型,而是对 C++ 代码的纯文本替换。 #define
是一个预处理指令,它甚至在您的代码开始编译之前运行。
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.
替换后 C++ 代码中的大小是您在那里拥有的 C++ 表达式或代码的大小。例如,如果你加上L
like后缀,102L
那么它是一个 long,否则没有后缀,只有一个 int。所以 x86 和 x64 上可能有 4 个字节,但这取决于编译器。
Perhaps the C++ standard's Integer literal section will clear it up for you (Section 2.13.1-2 of the C++03 standard):
也许 C++ 标准的整数文字部分会为您清除它(C++03 标准的第 2.13.1-2 节):
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
整数文字的类型取决于它的形式、值和后缀。如果它是十进制并且没有后缀,则它具有可以表示其值的这些类型中的第一个:int、long int;如果该值不能表示为 long int,则行为未定义。如果它是八进制或十六进制并且没有后缀,则它具有可以表示其值的这些类型中的第一个:int、unsigned int、long int、unsigned long int。如果以 u 或 U 为后缀,则其类型是这些类型中第一个可以表示其值的类型:unsigned int、unsigned long int。如果以 l 或 L 为后缀,则其类型是这些类型中第一个可以表示其值的类型:long int、unsigned long int。如果以 ul、lu、uL、Lu、Ul、lU、UL 或 LU 为后缀,则其类型为 unsigned long int
回答by Adam Shiemke
A plain integer is going to be implicitly cast to int
in all calculations and assignments.
将int
在所有计算和分配中隐式转换为普通整数。
#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
.
#define
只是告诉预处理器用其他东西替换对符号的所有引用。这与对代码执行全局查找替换并替换M_40
为40
.
回答by James Curran
A #define value has no size, specifically. It's just text substitution. It depends on the context of where (and what) is being substituted.
特别地,#define 值没有大小。这只是文本替换。这取决于替换位置(以及替换内容)的上下文。
In your example, where you use M_40
, the compile will see 40
, and usually treat it as in int
.
在您的示例中,在您使用 的地方M_40
,编译将看到40
,并且通常将其视为int
。
However, if we had:
但是,如果我们有:
void SomeFunc(long);
SomeFunc(M_40);
It will be treated as a long.
它将被视为长期。
回答by Simon Walker
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
当编译器看到它时。它没有自己的大小
回答by Carl Norum
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.
预处理器只进行简单的文本替换,因此常量在 a#define
中的事实无关紧要。C 标准说的是“每个常量都应该有一个类型,常量的值应该在其类型的可表示值范围内”。C++ 可能与此相差不大。