#define 值的大小

发布于 2024-09-08 05:52:24 字数 176 浏览 4 评论 0原文

如果值定义为

#define M_40 40

大小与 short 相同(2 字节)还是 char ( 1 字节)还是 int (4 字节)?

大小是否取决于您是 32 位还是 64 位?

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 int (4 bytes)?

Is the size dependent on whether you are 32-bit or 64-bit?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

毁我热情 2024-09-15 05:52:24

#define 没有大小,因为它不是类型,而是 C++ 代码中的纯文本替换。 #define 是一个预处理指令,它在代码开始编译之前运行。

替换后 C++ 代码的大小是 C++ 表达式或代码的大小。例如,如果您使用 L 后缀(如 102L),那么它会被视为 long,否则没有后缀,只是一个 int。所以在 x86 和 x64 上可能是 4 个字节,但这取决于编译器。

也许 C++ 标准的整数文字部分将为您清除它(C++03 标准的第 2.13.1-2 节):

整数文字的类型取决于
关于其形式、值和后缀。如果它
是十进制并且没有后缀,它有
其中第一种类型是
值可以表示:int、long
整数;如果该值不能为
表示为一个 long int,
行为未定义。如果是八进制
或十六进制并且没有后缀,它
有第一种类型,其中
其值可以表示为:int,
无符号整型、长整型、无符号长整型
国际。如果以 u 或 U 为后缀,则为
type 是这些类型中的第一个
其值可以表示为:
无符号整型,无符号长整型。如果它
后缀为 l 或 L,其类型为
第一种类型,其中
值可以表示:long int,
无符号长整型。如果是后缀
由 ul、lu、ul、Lu、UL、lU、UL 或 LU,
它的类型是unsigned long int

#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

德意的啸 2024-09-15 05:52:24

在所有计算和赋值中,普通整数将被隐式转换为 int

#define 只是告诉预处理器用其他东西替换对符号的所有引用。这与在代码上执行全局查找替换并将 M_40 替换为 40 相同。

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.

初见 2024-09-15 05:52:24

具体来说,#define 值没有大小。这只是文本替换。这取决于替换位置(以及替换内容)的上下文。

在您的示例中,如果使用 M_40,编译将看到 40,并且通常将其视为 int

但是,如果我们有:

void SomeFunc(long);

SomeFunc(M_40);

它将被视为 long。

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.

旧夏天 2024-09-15 05:52:24

预处理器宏在编译的预处理阶段实际上被交换。

例如,

#define N 5

int value = N;

代码将被交换

int value = 5;

当编译器看到代码时, 。它本身没有自己的尺寸

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

最美的太阳 2024-09-15 05:52:24

预处理器仅执行简单的文本替换,因此常量位于 #define 中这一事实并不重要。 C 标准规定的是“每个常量都应该有一个类型,并且常量的值应该在其类型的可表示值的范围内”。 C++ 可能不会有太大变化。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文