C Programming Tutorial 0/65 lessons ~6 min read Lesson 52

    Preprocessors

    The preprocessor runs before compilation — handles #include, #define, #ifdef, and conditional compilation.

    Course progress0%
    Focus
    9 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    The preprocessor runs before compilation — handles #include, #define, #ifdef, and conditional compilation.

    Understanding the topic

    #include Paste header contents into source.

    #define Text substitution macros and constants.

    #ifdef Include code only when macro is defined.

    • #include — Paste header contents into source.
    • #define — Text substitution macros and constants.
    • #ifdef — Include code only when macro is defined.

    Step-by-step explanation

    1. #include — Paste header contents into source.
    2. #define — Text substitution macros and constants.
    3. #ifdef — Include code only when macro is defined.

    Informative example

    Example program:

    c
    #include <stdio.h>
    #define PI 3.14159
    int main(void) {
    printf("%.5f\n", PI);
    return 0;
    }

    Output

    3.14159

    Execution workflow

    1Preprocessors — step by step
    1 / 3

    #include

    Paste header contents into source.

    Best practices

    • Enable warnings: gcc -Wall -Wextra -std=c11 source.c -o app
    • Give every variable a defined value before it is read.
    • Stay inside array bounds — C will not stop you from over-running a buffer.

    Common mistakes

    • Reading uninitialized storage — behavior is undefined.
    • Dismissing compiler warnings instead of fixing root causes.
    • Ignoring NULL returns from malloc, fopen, and similar APIs.

    Hands-on exercise

    Practice problems:

    • Define MAX macro
    • Header guards with #ifndef

    Summary

    Preprocessors in C — Directives processed before the compiler proper runs.

    Ready to mark this lesson complete?Track your journey across the entire course.