Skip to main content

Conditional Compilation in C

Conditional Compilation​

  • Conditional compilation allows certain sections of code to be included or excluded during compilation* based on the values of predefined constants or macro definitions.

  • This feature is typically used to create different versions of a program for different operating systems or hardware architectures.

  • For example: if a program needs to use different system calls on Windows and Linux, the code can use conditional compilation to include the appropriate system calls for each platform.

Syntax of Conditional Compilation​

The syntax of conditional compilation in C involves the use of preprocessor directives, which are commands that are executed by the C preprocessor before the code is compiled.

The most commonly used preprocessor directives for conditional compilation are:

  • #ifdef: tests whether a macro is defined or not, and includes the code that follows if the macro is defined.
  • #ifndef: tests whether a macro is not defined, and includes the code that follows if the macro is not defined.
  • #elif: specifies an alternative condition to test if the previous condition in an #ifdef or #ifndef block evaluates to false.
  • #else: specifies the code to be included if the previous condition in an #ifdef or #ifndef block evaluates to false.
  • #endif: marks the end of a conditional compilation block.

The general syntax for conditional compilation in C is as follows:

#ifdef MACRO
code to be included if MACRO is defined
#elif ANOTHER_MACRO
code to be included if ANOTHER_MACRO is defined
#else
code to be included if none of the above macros are defined
#endif

Types of Conditional Compilation​

There are two types of conditional compilation in C:

#if-#endif Directives:​

  • The #if-#endif directives allow you to test a constant expression, and include or exclude code based on the result of the test.

  • The #if-#endif block is similar to an if statement in C, but it is evaluated at compile-time rather than run-time.

The syntax for the #if-#endif directives is:

#if constant_expression
code to be included if the constant expression is true
#endif

#ifdef-#endif and #ifndef-#endif Directives:​

  • The #ifdef-#endif and #ifndef-#endif directives allow you to test whether a particular macro is defined or not, and include or exclude code based on the result of the test.

  • The #ifdef-#endif block includes the code if the macro is defined, while the #ifndef-#endif block includes the code if the macro is not defined.

The syntax for these directives is:

#ifdef macro
code to be included if macro is defined
#endif

#ifndef macro
code to be included if macro is not defined
#endif

Example of Conditional Compilation​

Here is an example of conditional compilation in C:

#include <stdio.h>

#define LINUX 1
#define WINDOWS 0

int main() {
#ifdef LINUX
printf("This code is for Linux\n");
#elif WINDOWS
printf("This code is for Windows\n");
#else
printf("This code is for other platforms\n");
#endif

return 0;
}

Explanation:

  • The code inside the #ifdef LINUX block will be compiled if the LINUX macro is defined, while the code inside the #elif WINDOWS block will be compiled if the WINDOWS macro is defined.

  • If neither macro is defined, the code inside the #else block will be compiled.

note

Multiple conditional compilation macros can be nested inside each other, allowing for more complex tests and conditions to be evaluated at compile-time.