Skip to main content

Lesson 2 of 5

Conditional Compilation - #ifdef, #if & Include Guards

Conditional compilation allows code to be included or excluded at the preprocessor stage, before the compiler sees it. Used for: platform-specific code, debug builds, feature flags, and include guards. Include guards (or `#pragma once`) prevent a header from being included multiple times in a single translation unit.

11 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Conditional Compilation - All Patterns

Conditional Compilation - All Patterns
/* ── Include guard - prevents double inclusion ──────────────
   Without guards: including "mylib.h" twice redefines all
   its types, causing 'redefinition of ...' errors.          */

/* config.h */
#ifndef CONFIG_H     /* if CONFIG_H is NOT defined */
#define CONFIG_H     /* define it, so the next include is a no-op */

/* ── Feature flags ──────────────────────────────────────── */
#define ENABLE_DEBUG    1
#define ENABLE_COLORS   1

/* ── Platform detection ─────────────────────────────────── */
#ifdef _WIN32
  #define OS_NAME "Windows"
  #define PATH_SEP '\'
#elif defined(__linux__)
  #define OS_NAME "Linux"
  #define PATH_SEP '/'
#elif defined(__APPLE__)
  #define OS_NAME "macOS"
  #define PATH_SEP '/'
#else
  #define OS_NAME "Unknown"
  #define PATH_SEP '/'
#endif

/* ── Numeric #if - more powerful than #ifdef ────────────── */
#if ENABLE_DEBUG
  #define LOG(fmt, ...) fprintf(stderr, "[LOG] " fmt "
", ##__VA_ARGS__)
#else
  #define LOG(fmt, ...) ((void)0)   /* compiles to nothing in release */
#endif

#endif /* CONFIG_H */

/* ── main.c - using conditional compilation ─────────────── */
#include <stdio.h>
/* #include "config.h" */

#define NDEBUG 0  /* 0=debug build, 1=release (disables assert) */
#include <assert.h>

/* Compile-time assertion (C11: _Static_assert, or gcc extension) */
/* _Static_assert(sizeof(int) == 4, "Require 32-bit int"); */

int main(void) {
    /* Platform detection */
    /* printf("OS: %s
", OS_NAME); */
    /* printf("Path separator: %c
", PATH_SEP); */

    /* LOG("Starting program, pid=%d", 1234); */
    /* In release build (ENABLE_DEBUG=0), LOG expands to nothing */

    /* ── #undef - remove a definition ──────────────────── */
    #define TEMP_MACRO 100
    printf("TEMP_MACRO = %d
", TEMP_MACRO);
    #undef TEMP_MACRO
    /* printf("%d", TEMP_MACRO); ← compile error: undefined */

    /* ── Variadic macros (C99) ───────────────────────────── */
    #define INFO(fmt, ...) printf("[INFO] " fmt "
", __VA_ARGS__)
    INFO("x=%d, y=%.2f", 10, 3.14);

    /* ── #pragma once - alternative to include guards ───── */
    /* #pragma once at top of header = same effect as #ifndef guard
       Supported by all modern compilers. Simpler to write.
       Not in the C standard, but universally supported.     */

    return 0;
}

Common Mistakes

  • Inconsistent include guard names - `#ifndef MY_LIB` in one file and `#define MYLIB` - a mismatch means the guard never fires. Always use the macro name that exactly matches: `#ifndef FILE_NAME_H` / `#define FILE_NAME_H`.
  • `#ifdef DEBUG` vs `#if DEBUG` - `#ifdef` checks if the macro is defined (regardless of value). `#if DEBUG` checks if the value is non-zero. If you `#define DEBUG 0`, `#ifdef DEBUG` fires! Use `#if DEBUG` for feature flags with 0/1 values.
  • Forgetting `#endif` - every `#if` / `#ifdef` / `#ifndef` needs a matching `#endif`. Long files with many conditionals are easy to mis-match - keep them minimal and clearly commented.

Tip

Tip

Practice Conditional Compilation ifdef if Include Guards in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Include guards prevent duplicate header definitions during multi-file compilation

Practice Task

Note

Practice Task - (1) Write a working example of Conditional Compilation ifdef if Include Guards from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Conditional Compilation ifdef if Include Guards is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.

Key Takeaways

  • Conditional compilation allows code to be included or excluded at the preprocessor stage, before the compiler sees it.
  • Inconsistent include guard names - `#ifndef MY_LIB` in one file and `#define MYLIB` - a mismatch means the guard never fires. Always use the macro name that exactly matches: `#ifndef FILE_NAME_H` / `#define FILE_NAME_H`.
  • `#ifdef DEBUG` vs `#if DEBUG` - `#ifdef` checks if the macro is defined (regardless of value). `#if DEBUG` checks if the value is non-zero. If you `#define DEBUG 0`, `#ifdef DEBUG` fires! Use `#if DEBUG` for feature flags with 0/1 values.
  • Forgetting `#endif` - every `#if` / `#ifdef` / `#ifndef` needs a matching `#endif`. Long files with many conditionals are easy to mis-match - keep them minimal and clearly commented.

Topics in This Module

0/5 done