| ← Previous | 🏠 Homepage | Next Chapter → |
#if & #elif – If your code has trust issues...
Think of #if like a “bouncer” at a party:
"If the condition is true, you can enter this part of the code."
#if AGE >= 18
printf("Welcome to the party!\n");
#else
printf("Go back to school, kid!\n");
#endif
You can also do:
#if LEVEL == HARD || MODE == CRAZY
// Challenge accepted
#endif
Use #elif to make it fancier:
#if ADAPTER == VGA
// VGA code
#elif ADAPTER == SVGA
// SVGA code
#else
// Potato mode (EGA)
#endif
#elif = fewer #endifs = cleaner code = less headache!
#undef – Break with a macro
If you don't want to use a macro anymore (maybe it's being annoying), just do:
#undef PI
Now PI is no longer defined. Like a ghosted ex—poof, gone!
#pragma – Secret commands to your compiler
Think of #pragma like whispering to the compiler:
"Hey... do something cool (or weird)."
#pragma startup and #pragma exit
main().#pragma startup fun1
#pragma exit fun2
void fun1() { printf("Warming up...\n"); }
void fun2() { printf("Shutting down...\n"); }
void main() {
printf("Main program running!\n");
}
Output:
Warming up...
Main program running!
Shutting down...
Note: The startup function runs before
main(), and the exit function runs after it ends. Like putting on shoes before going out—and taking them off after coming back.
#pragma warn – Mute those annoying warning messages
Example:
#pragma warn -rvl // suppress return value warning
#pragma warn -par // suppress unused parameter warning
#pragma warn -rch // suppress unreachable code warning
Now even if you write messy code, compiler will say:
“I saw nothing.”
But don’t do this forever—it’s like ignoring a leaky tap. One day…
Wrap-Up !
| Directive | What It Does |
|---|---|
#if, #elif, #else, #endif |
Chooses which code to compile |
#undef |
Erases a macro |
#pragma |
Sends special instructions to the compiler (e.g., startup, exit, suppress warnings) |
The preprocessor is like a code genie – it grants your wishes before compilation even begins.
Use its powers wisely, and it’ll make your coding life a lot more magical.
Need the last part of the chapter? Just shout "abracadabra"!
No comments:
Post a Comment