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

C Preprocessor Introduction

The preprocessor is a helper that prepares your code text before it is compiled, like an editor doing find-and-replace and pasting in other files. Lines starting with # are for it.
Syntax
c
#include <header.h>
#define NAME value

#ifdef NAME
// compiled only if NAME is defined
#endif

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 standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro GREETING (replaced by the preprocessor)
#define GREETING "Hello"
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf(GREETING);
	// Return 0 to signal that the program finished successfully
	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 standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("stdio.h contents are copied in before compiling");
	// Return 0 to signal that the program finished successfully
	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 standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro PI (replaced by the preprocessor)
#define PI 3.14
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%.2f", PI);
	// Return 0 to signal that the program finished successfully
	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 standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro DEBUG (replaced by the preprocessor)
#define DEBUG 1
// Program execution starts in main()
int main() {
	#if DEBUG
	// Print formatted text to the screen
	printf("Debug mode");
	#endif
	// Return 0 to signal that the program finished successfully
	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 standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro VALUE (replaced by the preprocessor)
#define VALUE 5
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", VALUE);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Thinking the preprocessor runs after compilation.
  2. Putting semicolons after preprocessor directives.
  3. Forgetting that macros are text substitution.
🔒

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.