宏在我的 C 代码中与整数和字符有何不同
how macro works different with integers and char in my C-code
我熟悉 "C" 语言中使用宏的条件编译,但有些代码让我感到困惑,我可以理解 CODE-1 的工作原理,没有任何内容分配给 X 因此 Y 被定义为5(其他条件)和打印 Y 我们将得到 5 作为输出。
但在 CODE-2
中与 CODE-1
非常相似,除了
"#if X == A"
输出为 3
的条件,我不明白它如何产生 3
的输出。谁能告诉我如何
"#if X == 3"
和
"#if X == A"`
使输出不同。
CODE-1
#include <stdio.h>
#if X == 3
#define Y 3
#else
#define Y 5
#endif
int main()
{
printf("%d", Y);
return 0;
}
//output : 5
CODE-2
#include <stdio.h>
#if X == A
#define Y 3
#else
#define Y 5
#endif
int main()
{
printf("%d", Y);
return 0;
}
//output : 3
I expect the output of CODE-2
to be 5
, but the actual output is 3
.
宏条件中不能进一步扩展为整数(不是宏)的标记被替换为 0。
Prior to evaluation, macro invocations in the list of preprocessing
tokens that will become the controlling constant expression are
replaced (except for those macro names modified by the defined unary
operator), just as in normal text. If the token defined is generated
as a result of this replacement process or use of the defined unary
operator does not match one of the two specified forms prior to macro
replacement, the behavior is undefined. After all replacements due to
macro expansion and the defined unary operator have been performed,
all remaining identifiers (including those lexically identical to
keywords) are replaced with the pp-number 0, and then each
preprocessing token is converted into a token. The resulting tokens
compose the controlling constant expression which is evaluated
according to the rules of 6.6.
由于在您的第二个代码段中既没有定义 X
也没有定义 Y
的原因,因此它等同于:
#if 0 == 0
#define Y 3
#else
#define Y 5
#endif
自然会解析到第一个分支。
我熟悉 "C" 语言中使用宏的条件编译,但有些代码让我感到困惑,我可以理解 CODE-1 的工作原理,没有任何内容分配给 X 因此 Y 被定义为5(其他条件)和打印 Y 我们将得到 5 作为输出。
但在 CODE-2
中与 CODE-1
非常相似,除了
"#if X == A"
输出为 3
的条件,我不明白它如何产生 3
的输出。谁能告诉我如何
"#if X == 3"
和
"#if X == A"`
使输出不同。
CODE-1
#include <stdio.h>
#if X == 3
#define Y 3
#else
#define Y 5
#endif
int main()
{
printf("%d", Y);
return 0;
}
//output : 5
CODE-2
#include <stdio.h>
#if X == A
#define Y 3
#else
#define Y 5
#endif
int main()
{
printf("%d", Y);
return 0;
}
//output : 3
I expect the output of
CODE-2
to be5
, but the actual output is3
.
宏条件中不能进一步扩展为整数(不是宏)的标记被替换为 0。
Prior to evaluation, macro invocations in the list of preprocessing tokens that will become the controlling constant expression are replaced (except for those macro names modified by the defined unary operator), just as in normal text. If the token defined is generated as a result of this replacement process or use of the defined unary operator does not match one of the two specified forms prior to macro replacement, the behavior is undefined. After all replacements due to macro expansion and the defined unary operator have been performed, all remaining identifiers (including those lexically identical to keywords) are replaced with the pp-number 0, and then each preprocessing token is converted into a token. The resulting tokens compose the controlling constant expression which is evaluated according to the rules of 6.6.
由于在您的第二个代码段中既没有定义 X
也没有定义 Y
的原因,因此它等同于:
#if 0 == 0
#define Y 3
#else
#define Y 5
#endif
自然会解析到第一个分支。