C Preprocessor Introduction

C Preprocessor Introduction – Macros, Includes, and More

The preprocessor in C is a tool that runs before your code is compiled. It handles things like including header files, defining constants, and conditionally compiling parts of your program.

Preprocessor commands are also called directives and they all begin with a # symbol. These directives are not C code, but instructions for the compiler.

📋 Common Preprocessor Directives

  • #include – Inserts contents of a file
  • #define – Defines macros or constants
  • #ifdef / #ifndef / #endif – Conditional compilation

🧪 Example: Using #define and #include

#include <stdio.h>

#define PI 3.14159

int main() {
    printf("Value of PI: %f\n", PI);
    return 0;
}

Try It Now

📘 What Happens Under the Hood?

The preprocessor will literally replace the macro PI with 3.14159 before your code gets compiled. It also pulls in the content of <stdio.h> so functions like printf() work.

🧠 Why Use Preprocessor Directives?

  • To define constants (e.g., #define MAX 100)
  • To include external files (#include)
  • To conditionally compile code for different platforms

🎉 Summary

The preprocessor is like a code wizard—it handles useful replacements and setups before your program is even compiled. Mastering it is key to writing clean, flexible C programs!