C Preprocessor Directives

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is Preprocessor Directives?

A preprocessor directive is a line starting with #. Directives are not C statements: they end at the end of the line and are handled before compilation. The main ones are #include, #define / #undef, #if / #ifdef / #ifndef / #else / #elif / #endif, #error, #pragma, and #line.

Remember
#include <stdio.h>     /* paste a header      */
#define NAME value      /* define a macro      */
#undef  NAME            /* remove a macro      */
#ifdef NAME ... #endif  /* conditional blocks  */
#error "message"        /* stop compilation    */

Cheat Sheet

Keep these facts handy while you work with Preprocessor Directives in C.

TopicDetail
Start# as first non-space character
EndEnd of line (use \ to continue)
ScopeFrom definition to end of file or #undef
Inspectgcc -E file.c

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: #include Directive.

Example 1 — define, ifdef, and undef

The preprocessor decides which printf lines exist.

C
#include <stdio.h>

#define DEBUG

int main(void) {
#ifdef DEBUG
    printf("debug build\n");
#endif

#undef DEBUG

#ifndef DEBUG
    printf("debug off\n");
#endif
    return 0;
}

How It Works

  1. #define DEBUG defines the macro with an empty value.
  2. #ifdef DEBUG is true, so the first message is compiled in.
  3. After #undef, #ifndef DEBUG is true.

Edge Cases & Pitfalls

Check these before relying on Preprocessor Directives in real programs.

space

Spaces before the hash

Leading whitespace is allowed, but nothing else can precede # on the line.

cont

Line continuation

A backslash must be the very last character on the line; trailing spaces break it.

Key Takeaways

  • Lines: directives are line-oriented.
  • Order: processed top to bottom.
  • Debug: use gcc -E.

One line: A preprocessor directive is a line starting with #.

Frequently Asked Questions

No. A directive ends at the end of its line. A stray semicolon becomes part of the replacement text.
Before compilation. It transforms the source text, and the compiler then sees the result.
Continue with "C #include Directive" next. Paste the contents of a header file into your source file. The C Preprocessor hub lists every topic in order.

Next: #include Directive

Paste the contents of a header file into your source file.

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.

8 people found this page helpful