Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

c - When will this execute?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...