C Preprocessor

Intermediate
15 min read
Updated: Nov 2026
1 example
9 guides

Definition and Usage

The C preprocessor runs before the compiler. It processes lines that start with # (directives), expands macros, includes header files, and keeps or removes blocks of code with conditional compilation. This section covers #include, #define, object-like and function-like macros, #if / #ifdef, header guards, predefined macros, and #error / #pragma.

Macros are text, not code.

The preprocessor knows nothing about types or scope. Parenthesize macro parameters and the whole expansion to avoid surprising precedence.

Learning Path

Work through the stages in order, or jump to a topic from the index below (9 tutorials).

C Examples

A quick taste of preprocessor. Compile with gcc file.c -std=c11 -Wall -o out.

Example 1 — Macros and conditional compilation

A constant, a function-like macro, and a debug switch.

C
#include <stdio.h>

#define DEBUG 1
#define SQUARE(x) ((x) * (x))

int main(void) {
    int n = 4;
#if DEBUG
    printf("debug: n = %d\n", n);
#endif
    printf("%d squared = %d\n", n, SQUARE(n));
    return 0;
}

How It Works

  1. #if DEBUG keeps the first printf because DEBUG is 1.
  2. SQUARE(n) expands to ((n) * (n)) before compilation.
  3. Setting DEBUG to 0 removes the debug line from the program entirely.

C Preprocessor Topics

Every guide in this section with a one-line summary (9 tutorials).

Directives and macros

5 tutorials

Include files and define constants and macros.

TopicDescriptionTutorial
Preprocessor directivesAn overview of #include, #define, #if, #pragma and other # lines.Try it
#includePaste the contents of a header file into your source file.Try it
#defineDefine named constants and macros with #define.Try it
Object-like macrosMacros without parameters: constants, strings, and expressions.Try it
Function-like macrosMacros with parameters, stringizing (#), and token pasting (##).Try it

Controlling compilation

4 tutorials

Conditional builds, header guards, and diagnostics.

TopicDescriptionTutorial
Conditional compilationInclude or exclude code with #if, #ifdef, #elif, #else, and #endif.Try it
Header guardsPrevent a header from being included more than once.Try it
Predefined macrosUse __LINE__, __FILE__, __DATE__, __STDC__ and friends.Try it
#error and #pragmaStop the build with #error and give compiler hints with #pragma.Try it

Edge Cases & Pitfalls

Common mistakes when learning preprocessor in C.

paren

Missing parentheses

#define SQ(x) x*x makes SQ(a+1) become a+1*a+1. Wrap every parameter and the whole expansion.

side

Repeated evaluation

SQUARE(i++) evaluates i++ twice. Use an inline function instead.

semi

Trailing semicolons

#define N 10; leaks the semicolon into every use.

Key Takeaways

  • Text: macros are substituted before compilation.
  • Guard: protect headers.
  • Prefer: const, enum, and inline when they fit.

One line: The C preprocessor runs before the compiler.

Frequently Asked Questions

It is a text-processing step before compilation that handles #include, #define, conditional compilation, and other directives.
A macro is replaced by text before compilation and has no type checking or call overhead. A function is compiled code that is called at run time and evaluates arguments once.
Compile with gcc -E file.c to print the code after preprocessing.

Next: Preprocessor Directives

An overview of #include, #define, #if, #pragma and other # lines.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful