When will this execute?

浪子不回头ぞ 提交于 2019-12-13 10:13:47

问题


I have a C code:

... 
void caller() {
    #define YES 1
    #define NO 0
}
...

Will the both #define lines execute when caller is called/executed, or will they execute at compile-time only.


回答1:


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() {
}

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);
}


来源:https://stackoverflow.com/questions/43862311/when-will-this-execute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!