← Back to C Course | Chapter 11: Preprocessor | Lesson 1 of 5

C Preprocessor Introduction

What is the Preprocessor?

This is a purely textual process -- the preprocessor has no understanding of C syntax or semantics, it just performs pattern-based find-and-replace and file-inclusion operations before the real compiler ever sees the code.

Example: What is the Preprocessor?

c
#include <stdio.h>
#define GREETING "Hello"
int main() {
	printf(GREETING);
	return 0;
}

Header Expansion

This is effectively a copy-paste operation: the entire target header file's text is inserted in place of the #include line, which is why including the same header twice (without protection) can cause duplicate-definition errors.

Example: Header Expansion

c
#include <stdio.h>
int main() {
	printf("stdio.h contents are copied in before compiling");
	return 0;
}

Macro Substitution

Because this is pure text substitution rather than a function call, macros have no type checking and can behave unexpectedly with complex arguments -- this is a common source of subtle bugs compared to using an actual function.

Example: Macro Substitution

c
#include <stdio.h>
#define PI 3.14
int main() {
	printf("%.2f", PI);
	return 0;
}

Conditional Preparation

This lets you compile different versions of the same source file for different platforms or configurations, without maintaining separate copies of the code, by defining or omitting certain macros at compile time.

Example: Conditional Preparation

c
#include <stdio.h>
#define DEBUG 1
int main() {
	#if DEBUG
	printf("Debug mode");
	#endif
	return 0;
}

The Compilation Flow

This intermediate expanded output (sometimes visible with a compiler flag like -E) contains no macros, comments, or conditional blocks left -- just plain C code ready for the compiler's lexer and parser to process.

Example: The Compilation Flow

c
#include <stdio.h>
#define VALUE 5
int main() {
	printf("%d", VALUE);
	return 0;
}
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.