Labs ICT
โญ Pro Login

Preprocessor

Before your C code is even compiled, the preprocessor runs through it and makes changes. It's a text-replacement engine that handles includes, defines constants, and conditionally includes or excludes code.

#include โ€” Bringing in Files

#include literally pastes the contents of another file into yours. Angle brackets (<>) search system directories; quotes ("") search your project first.


#include 
#include "myheader.h"
int main() {
  printf("Includes work!\n");
  return 0;
}
    
Try it Yourself โ†’

#define โ€” Constants and Macros

#define creates a macro โ€” a name that gets replaced with whatever value you give it. No semicolon, no type, just pure text substitution before compilation.


#include 
#define PI 3.14159
#define AREA(r) (PI * (r) * (r))
int main() {
  double radius = 5.0;
  printf("Area: %.2f\n", AREA(radius));
  return 0;
}
    
Try it Yourself โ†’

#ifdef and #ifndef โ€” Conditional Compilation

These let you include or skip code based on whether a macro is defined. Perfect for platform-specific code, debug builds, or preventing double inclusion.


#include 
#define DEBUG
int main() {
  int x = 42;
  #ifdef DEBUG
    printf("DEBUG: x = %d\n", x);
  #endif
  printf("Hello World\n");
  return 0;
}
    
Try it Yourself โ†’

Include Guards

When multiple files include the same header, you'd get duplicate definitions. An include guard โ€” using #ifndef, #define, #endif โ€” ensures each header is processed only once.


#ifndef MYHEADER_H
#define MYHEADER_H
#define VERSION "1.0.0"
void greet() {
  printf("Hello from header!\n");
}
#endif
    
Try it Yourself โ†’

๐Ÿงช Quick Quiz

Which directive includes a header file?