Loading...

C essentialsModular C

The preprocessor: #include, #define & #ifdef

8 min lesson

Next
  • The preprocessor is a step that runs before the compiler, editing your source text by handling lines that start with #.
  • It matters because it pulls in library code, defines constants, and lets you include or exclude code based on conditions.
  • Think of the preprocessor as a careful assistant who reads your notes and rewrites them before the chef (the compiler) sees them.
  • #include pastes the contents of another file at that spot, usually a header file like <stdio.h>.
  • #define creates a simple text replacement; every occurrence of the name is replaced with the value before compiling.
  • #ifdef and #ifndef check whether a name is defined and include code only when the condition is true; #endif closes the block.
  • The computer does no type checking at this stage; it is plain text substitution, which is powerful but easy to misuse.
  • The syntax is: #include <stdio.h>, #define MAX 100, #ifdef DEBUG ... #endif.
  • A realistic use: turning debug prints on or off with a DEBUG macro, and including headers for the functions you use.
  • A surprising detail: #define has no semicolon and no type; it is not a variable, just text that gets pasted in.
  • The example program uses #define for a maximum value and #ifdef to print extra debug output only when DEBUG is defined.
Keep in mind
  • Remember that the preprocessor runs before the compiler and works on text, not types.
  • Use #include for headers and #define for simple constants or flags.
  • Wrap optional code in #ifdef ... #endif so it can be switched on or off.
  • Prefer const over #define for typed constants in modern C.
  • The preprocessor is a powerful first step. Nice work!
main.c
#include <stdio.h>

// Define a constant the preprocessor will paste in
#define MAX_SCORE 100

// Define DEBUG to turn on the extra print below
#define DEBUG

int main(void) {
    // Use the macro like a normal constant
    int score = 87;
    // Print the score and the maximum
    printf("Score: %d / %d\n", score, MAX_SCORE);

#ifdef DEBUG
    // This block is kept only because DEBUG is defined
    printf("Debug: score is %d\n", score);
#endif

    return 0;
}

What happens: Score: 87 / 100 Debug: score is 87

Breaking it down

Include line

#include <stdio.h>

Pastes the declarations of printf and other I/O functions into your file. Without it, the compiler does not know printf.

Define constant

#define MAX_SCORE 100

Every MAX_SCORE in the file is replaced with 100 before compiling. No semicolon and no type are involved.

Define a flag

#define DEBUG

Marks the name DEBUG as defined. It has no value; it is used only for #ifdef checks.

Conditional block

#ifdef DEBUG ... #endif

Keeps the enclosed lines only when DEBUG is defined. Otherwise the block is removed before compiling.

Common preprocessor directives

DirectivePurposeExample
#includePaste a header file#include <stdio.h>
#defineDefine a macro#define MAX 100
#ifdefCompile if defined#ifdef DEBUG
#ifndefCompile if not defined#ifndef HEADER_H
#endifClose conditional#endif
#undefRemove a definition#undef DEBUG

A helpful analogy

The preprocessor is like an assistant who edits your recipe before the chef sees it. If you write 'use MAX eggs', the assistant counts and writes the actual number. If you write 'only if the guest is vegetarian', the assistant either keeps or removes that step before cooking starts.

Nesting rules

  • 1.Preprocessor directives start with # at the beginning of a line and have no semicolon.
  • 2.Every #if, #ifdef or #ifndef must be closed with #endif.
  • 3.#define does plain text replacement; wrap arguments in parentheses when writing function-like macros.
  • 4.Use #ifndef to guard headers so they are included only once.

Common mistakes

Putting a semicolon after #define

Avoid

#define MAX 100; // semicolon pasted

Use instead

#define MAX 100

The semicolon becomes part of the replacement, so int x = MAX; turns into int x = 100;; which is a syntax error.

Forgetting #endif

Avoid

#ifdef DEBUG printf("x");

Use instead

#ifdef DEBUG printf("x"); #endif

The compiler reports an unterminated conditional. Every conditional block must be closed.

Expecting #define to respect scope

Avoid

#define N 5 // affects whole file

Use instead

const int N = 5; // scoped

#define is not scoped to a function; it applies from that line to the end of the file or until #undef.

Quick tips

  • •Name macros in ALL_CAPS so they stand out from variables.
  • •Use #ifdef DEBUG to turn debug prints on and off without deleting code.
  • •Remember that #define is text replacement, not a real variable.
  • •Try removing #define DEBUG and rerun to see the debug line disappear.
  • •Compile with -DDEBUG to define DEBUG from the command line.
Previous
Next