Skip to main content

Preprocessor directives

Short answer

Preprocessor directives (lines starting #) run before compilation. They are needed to include headers (#include), define constants/macros (#define), and enable conditional compilation for portability and debugging.

What are preprocessor directives? Why are they needed?

Preprocessor directives are lines beginning with # that are processed by the preprocessor before the actual compilation starts. They are not C statements and do not end with a semicolon.

Common directives: #include (include a file), #define (macro or constant), #undef, #ifdef / #ifndef / #endif (conditional compilation), #pragma.

They are needed to:

  • include header files such as stdio.h so library functions can be used;
  • define symbolic constants and macros (#define PI 3.14159), making programs readable and easy to change;
  • compile portions of code conditionally for different machines or debugging;
  • make programs portable and shorter.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 3 questions →