Preprocessor
Include Guard Pattern
Include guards define a value once even if guarded text is seen again.
guard macro
`#ifndef` checks whether a guard name has already been defined.
one definition
The guarded block supplies one default value for the rest of the source.
expanded value
The executable trace uses the guarded value after preprocessing.
Include Guard Pattern
include_guard_pattern.c
Replay: real traced execution (multi-file project)
#include <stdio.h>
#ifndef CONFIG_LIMIT
#define CONFIG_LIMIT 6
#endif
int main(void) {
int used = 4;
int limit = 6;
int remaining = limit - used;
printf("remaining=%d\n", remaining);
return 0;
}
#include <stdio.h>
#ifndef CONFIG_LIMIT
#define CONFIG_LIMIT 6
#endif
int main(void) {
int used = 2;
int limit = 6;
int remaining = limit - used;
printf("remaining=%d\n", remaining);
return 0;
}
#include <stdio.h>
#ifndef CONFIG_LIMIT
#define CONFIG_LIMIT 6
#endif
int main(void) {
int used = 8;
int limit = 6;
int remaining = limit - used;
printf("remaining=%d\n", remaining);
return 0;
}
used ← 4, limit ← 6, remaining ← 2
7int main(void) {8 int used→ 4 = 4; //@used=2, 89 int limit→ 6 = 6;10 int remaining→ 2 = limit6 - used4;1112 printf("remaining=%d\n", remaining2);13 return 0;14}outputremaining=2
used ← 2, limit ← 6, remaining ← 4
7int main(void) {8 int used→ 2 = 2;9 int limit→ 6 = 6;10 int remaining→ 4 = limit6 - used2;1112 printf("remaining=%d\n", remaining4);13 return 0;14}outputremaining=4
used ← 8, limit ← 6, remaining ← -2
7int main(void) {8 int used→ 8 = 8;9 int limit→ 6 = 6;10 int remaining→ -2 = limit6 - used8;1112 printf("remaining=%d\n", remaining-2);13 return 0;14}outputremaining=-2