C语言 C 预处理器#if 表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6362622/
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
C preprocessor #if expression
提问by Rohit Jain
I am a bit confused on the type of expression we can use with the #IF preprocessor in the C language. I tried the following code, and it isn't working. Please explain and provide examples for expressions that can be used with the preprocessor.
我对我们可以在 C 语言中与 #IF 预处理器一起使用的表达式类型感到有些困惑。我尝试了以下代码,但它不起作用。请解释并提供可与预处理器一起使用的表达式的示例。
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int c=1;
#if c==1
#define check(a) (a==1)?a:5
#define TABLE_SIZE 100
#endif
int main()
{
int a = 0, b;
printf("a = %d\n", a);
b = check(a);
printf("a = %d %d\n", a, TABLE_SIZE);
system("PAUSE");
return 0;
}
回答by Michael Burr
The preprocessor cannot use variables from the C program in expressions - it can only act on preprocessor macros. So when you try to use cin the preprocessor you don't get what you might expect.
预处理器不能在表达式中使用来自 C 程序的变量——它只能作用于预处理器宏。因此,当您尝试c在预处理器中使用时,您不会得到预期的结果。
However, you also don't get an error because when the preprocessor tries to evaluate an identifier that isn't defined as a macro, it treats the identifier as having a value of zero.
但是,您也不会收到错误消息,因为当预处理器尝试评估未定义为宏的标识符时,它会将标识符视为具有零值。
So when you hit this snippet:
所以当你点击这个片段时:
#if c==1
#define check(a) (a==1)?a:5
#define TABLE_SIZE 100
#endif
The cused by the preprocessor has nothing to do with the variable cfrom the C program. The preprocessor looks to see if there's a macro defined for c. Since there isn't, it evaluates the following expression:
该c预处理器采用了无关变量c从C程序。预处理器查看是否有为c. 由于没有,它计算以下表达式:
#if 0==1
which is false of course.
这当然是错误的。
Since you don't appear to use the variable cin your program, you can do the following to get behavior in line with what you're trying:
由于您似乎没有c在程序中使用该变量,您可以执行以下操作以使行为符合您的尝试:
#define C 1
#if C==1
#define check(a) (a==1)?a:5
#define TABLE_SIZE 100
#endif
(Note that I also made the macro name uppercase in keeping with convention for macro names.)
(请注意,我还将宏名称设为大写以符合宏名称的约定。)
回答by nmichaels
The preprocessor is run on the text, before any compilation is done. It doesn't know how to parse C. What you probably wanted instead of int c=1;was
在任何编译完成之前,预处理器在文本上运行。它不知道如何解析C.什么你可能想,而不是int c=1;为
#define C 1
and the test works the way you had it:
并且测试按照您的方式进行:
#if C == 1
The key here is that this is all defined beforecompile time. The preprocessor doesn't care about C variables, and certainly doesn't care what their values are.
这里的关键是这都是在编译之前定义的。预处理器不关心 C 变量,当然也不关心它们的值是什么。
Note that the convention is to have preprocessor macro names defined in ALL_CAPS.
请注意,约定是在ALL_CAPS.
回答by Dave Rager
The preprocessor does not evaluate C variables. It "preprocesses" the source code before it is compiled and thus has its own language. Instead do this:
预处理器不评估 C 变量。它在编译之前对源代码进行“预处理”,从而拥有自己的语言。而是这样做:
#define c 1
#if c==1
#define check(a) (a==1)?a:5
#define TABLE_SIZE 100
#endif
...
回答by Clifford
In your example cis a compiler generated symbol, chas no value until run-time, whereas preprocessor expressions are evaluated at build-time(in fact as the name suggests before the compiler processes the code), so can only operate on pre-processor symbols which doexist at build time.
在您的示例中c是编译器生成的符号,c直到运行时才有价值,而预处理器表达式在构建时进行评估 (实际上顾名思义是在编译器处理代码之前),因此只能对预处理器符号进行操作它做在构建时存在。
Moreover such expressions must be compile time constants, or in fact more exactly preprocessing time constant, since compiler constant expressions such as sizeof(...)for example are also not defined during pre-processing.
此外,此类表达式必须是编译时间常量,或者实际上更准确地说是预处理时间常量,因为sizeof(...)在预处理期间也未定义编译器常量表达式。

