0Pricing
C Academy · Lesson

Conditional Compilation

Learn how to use preprocessor directives to compile code conditionally.

Conditional Compilation is a free C Academy lesson on CoddyKit — 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, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

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

2

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.

3

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;
}

4

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

5

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;
}

6

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

7

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;
}

8

9

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.

10

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

Frequently asked questions

Is the “Conditional Compilation” lesson free?

Yes — the full text of “Conditional Compilation” is free to read here on the web, and the C Academy course includes 3 lessons in total. 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.

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; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.

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