什么时候执行?
问题描述:
我有一个C代码:
...
void caller() {
#define YES 1
#define NO 0
}
...
#define
这两条线将在调用者
被调用/执行时执行,还是在编译时执行
Will the both #define
lines execute when caller
is called/executed, or will they execute at compile-time only.
答
preprcessor宏不执行,它们只是代码的命名片段,将被预处理器替换使用它们的内容。在此处了解更多信息。
The prerpcessor macros don't execute, they are just named fragments of the code which will be replaced by the preprocessor to theirs content if you use them. Read more about preprocessor macros here.
因此,在预处理之后,您的代码将是:
So, after preprocessing, your code will be:
void caller() {
}
假设您在之后使用 YES
宏您 #define
它:
Let assume you use the YES
macro after you #define
it:
#define YES 1
#define NO 0
void caller() {
printf("My answer is: %d", YES);
}
在预处理之后,上面的代码将如下:
After preprocessing the code above will be the following:
void caller() {
printf("My answer is: %d", 1);
}