Preprocessor
Conditional Flags
Conditional compilation includes one branch of source before the compiler runs.
compile-time branch
`#if` chooses source text at build time, not while the program is running.
runtime selector
The selector changes data that flows through the compiled branch.
Conditional Flags
conditional_flags.c
Replay: real traced execution (multi-file project)
#include <stdio.h>
#define USE_BONUS 1
int main(void) {
int base = 10;
#if USE_BONUS
int score = base + 3;
#else
int score = base;
#endif
printf("score=%d\n", score);
return 0;
}
#include <stdio.h>
#define USE_BONUS 1
int main(void) {
int base = 5;
#if USE_BONUS
int score = base + 3;
#else
int score = base;
#endif
printf("score=%d\n", score);
return 0;
}
#include <stdio.h>
#define USE_BONUS 1
int main(void) {
int base = 12;
#if USE_BONUS
int score = base + 3;
#else
int score = base;
#endif
printf("score=%d\n", score);
return 0;
}
base ← 10, score ← 13
5int main(void) {6 int base→ 10 = 10; //@base=5, 127#if USE_BONUS8 int score→ 13 = base10 + 3;9#else10 int score = base;11#endif1213 printf("score=%d\n", score13);14 return 0;15}outputscore=13
base ← 5, score ← 8
5int main(void) {6 int base→ 5 = 5;7#if USE_BONUS8 int score→ 8 = base5 + 3;9#else10 int score = base;11#endif1213 printf("score=%d\n", score8);14 return 0;15}outputscore=8
base ← 12, score ← 15
5int main(void) {6 int base→ 12 = 12;7#if USE_BONUS8 int score→ 15 = base12 + 3;9#else10 int score = base;11#endif1213 printf("score=%d\n", score15);14 return 0;15}outputscore=15