C Academy · Lesson

Conditional Compilation

Learn how to use preprocessor directives to compile code conditionally.

Lesson 3 of 310 steps

Conditional Compilation is a free C Academy lesson on CoddyKit. This is lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, and your progress syncs across the web and the CoddyKit app. The C Academy course includes 3 lessons in total.

Introduction to Conditional Compilation

Conditional Compilation

Conditional compilation allows code to be included or excluded based on specific conditions.

In this lesson, you will learn:

  • How to use #ifdef and #ifndef for conditional compilation.
  • How to use #if, #else, and #endif to control compilation flow.
  • How conditional compilation helps create platform-independent code.
Conditional Compilation — illustration 1

Using #ifdef and #ifndef

The #ifdef directive includes code only if a macro is defined.

Example:

#ifdef DEBUG printf("Debug mode enabled\n"); #endif

The #ifndef directive works oppositely, including code only if a macro is NOT defined.

Example: Using #ifdef

This program demonstrates how to enable debugging messages only when DEBUG is defined.

#include <stdio.h>

#define DEBUG

int main() {
    #ifdef DEBUG
        printf("Debugging mode enabled\n");
    #endif
    printf("Program running...\n");
    return 0;
}

Using #if, #else, and #endif

The #if directive allows conditional compilation based on expressions.

Example:

#if VERSION >= 2 printf("Version 2 or higher\n"); #else printf("Old version\n"); #endif

Example: Using #if and #else

This program demonstrates version-based compilation using #if and #else.

#include <stdio.h>

#define VERSION 2

int main() {
    #if VERSION >= 2
        printf("Version 2 or higher\n");
    #else
        printf("Old version\n");
    #endif
    return 0;
}

Using #elif for Multiple Conditions

The #elif directive checks multiple conditions.

Example:

#if VERSION == 1 printf("Version 1\n"); #elif VERSION == 2 printf("Version 2\n"); #else printf("Unknown version\n"); #endif

Example: Using #elif

This program demonstrates how to handle multiple compilation conditions using #elif.

#include <stdio.h>

#define VERSION 2

int main() {
    #if VERSION == 1
        printf("Version 1\n");
    #elif VERSION == 2
        printf("Version 2\n");
    #else
        printf("Unknown version\n");
    #endif
    return 0;
}

Advantages of Conditional Compilation

Conditional compilation is useful for:

  • Creating platform-independent code.
  • Enabling debugging features without modifying the code.
  • Compiling different versions of a program.

Summary

In this lesson, you learned:

  • How to use #ifdef and #ifndef to check macro definitions.
  • How to use #if, #else, and #elif for conditional compilation.
  • The advantages of conditional compilation in creating flexible programs.

This concludes the Preprocessor Directives and Macros section in C!

Conditional Compilation — illustration 10
Free to start

Learn C with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
39
Lessons
144

Frequently Asked Questions

Is the “Conditional Compilation” lesson free?

Yes — the full text of “Conditional Compilation” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO. The C Academy course includes 3 lessons in total.

What will I learn in “Conditional Compilation”?

Learn how to use preprocessor directives to compile code conditionally. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 3.

How long does the “Conditional Compilation” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Understanding the C Preprocessor
  2. Macros and Inline Functions
  3. Conditional Compilation
← Back to C Academy