Conditional compilation directives in C are preprocessing instructions that allow parts of the program to be included or excluded based on the conditions of the execution. These instructions are processed before the actual compilation of the code.

Basic conditional compilation directives in C:

#if:  The pre-processor directive #if tests if a certain condition is true. If the condition calculates to true, the compiler includes the code between the #if and the following #endif, #else, or #elif directive.
#ifdef:  Checks if this macro has been defined. If a macro is defined, the code following #ifdef is compiled to #endif, #else, or #elif.
#ifndef:  Unlike #ifdef. Checks if a macro is undefined. If the macro is not defined or stated, the code after #ifndef is compiled.
#else:   It is used with #if, #ifdef or #ifndef. If the previous condition is false, the compiler includes the following code #else.
#elif:  It is short form of "else if". It permits for many conditions expression. If the previous #if, #ifndef, or #ifdef is false and the condition in #elif is true, the code following #elif is compiled.
#endif:  Text that begins with #if, #ifdef, #ifndef, #else, or #elif marks the end of a conditional compilation block.
#undef:  Used to undefine macros.
#define:   Directive is used to define macros, which can further be used in conditional compilation.


1. #ifndef Conditional Compilation Example in C

The #ifndef directive in C is a preprocessor command that checks whether a macro is undefined. It is "unless specified". If a specific macro is not defined, the code under #ifndef associated with #endif (or an #else, if present) will be included in the compilation process. This is often used to prevent double-inclusion of header files or to conditionally include code.
Here is an example that demonstrates the use of #ifndef: In this example, we will create a simple header file and use #ifndef to ensure that it is only included once in the build process.
demo_header.h:


 // Check if DEMO_HEADER_H is not defined
 #ifndef DEMO_HEADER_H
 #define DEMO_HEADER_H
 // header file contents
 void printDemo() {
 printf("Welcome to C world!\n");
 }
 #endif
 //main.c:
 #include <stdio.h>
 #include "demo_header.h"
 #include "demo_header.h" // Includs the header file again
 int main() {
 printDemo(); // Function from demo_header.h
 return 0;
}

In demo_header.h, the #ifndef directive checks if DEMO_HEADER_H is not defined. If it’s not defined (which is true the first time the header is included), it defines DEMO_HEADER_H and includes the contents of the header file. The next time this header file is included, DEMO_HEADER_H will already be defined, so the contents of the header file will be skipped, preventing multiple definitions of the printDemo() function.


2. #else Conditional Compilation Directive Example in C

#else is a directive in the C programming language that helps in providing statements that need to be executed when the given conditions are false using the #if, #ifdef, or #ifndef directives. #else directives provide alternative statements to execute once the condition specified in these directives becomes false.
It is part of the preprocessor directive because it is automatically called by the compiler before the actual compilation begins. Before compiling a C program, the source code is processed by the compiler, a process called preprocessing. All commands used for preprocessor are known as preprocessor directives and all preprocessor directives are defined using #.
Syntax of #else in C:
A preprocessor is a feature provided in C to process the source code written by the programmer before it is actually compiled. Before passing the program through the preprocessor, the compiler passes the code through the preprocessor, where the C program looks for specific instructions such as directives, known as preprocessor directives, that can be easily understood by the preprocessor. This preprocessor directive must begin with a (#) sign.
Preprocessor is a part of the compiler that performs necessary operations on the given code before the compiler compiles it. Transformations made by the preprocessor are words that indicate that the output of the preprocessor is in the form of text.

C program to implement #else

 #include <stdio.h>
 //#define AGE 23
 int main()
 {
  #ifdef AGE   //executes the code if AGE is present
  printf("My age is %d years now.\n", AGE);
  #else
  printf("The age value mentioned is not as expected to proceed!..\n");
  #endif
    printf("This is simple program to demonstrate #else...\n");
  return 0;
 }


3. #elif Conditional Compilation Directive Example in C
The #elif directive in C language is portion of conditional compilation, which permits you to define multiple conditional divisions in your code. It stands for "else if" and is used with #if, #ifdef, #ifndef and #else directives to create complex conditional compilation situations. Here's an example to show how #elif is used:

 #include <stdio.h>
 // Define macros to denote different situations
 #define ANDROID
 // #define LINUX
 // #define CentOs
 int main()
  {
 printf("Starting program...\n");
 #ifdef LINUX
 printf("Running on Linux.\n");
 // Linux-specific code goes here
 #elif defined(ANDROID)
 printf("Running on android.\n");
 // Android-specific code goes here
 #elif defined(CentOs)
 printf("Running on CentOs.\n");
 // CentOs -specific code goes here
 #else
 printf("Operating System not recognised.\n");
 #endif
 printf("Program running...\n");
 return 0;
 }


In this example:
• The #ifdef LINUX block will compile if LINUX is defined.
• If LINUX is not defined, but ANDROID is defined (#elif defined(ANDROID)), then the Linux-specific code block will compile.
• Similarly, the CentOs -specific code block will compile if CentOs is defined (#elif defined(CentOs)).
• If nothing of these macros are defined, the #else block will compile, representing an unknown operating system.
The #elif directive allows more fine-grained control and helps write more readable code for scenarios with multiple scenarios. This is especially useful in projects that need to be compiled in different environments with exact patterns for each.

4.#endif Conditional Compilation Directive Example in C
The #endif directive in C language is used with compilation directives such as #if, #ifdef and #ifndef. It results the end of a conditional directive block. These instructions are part of the C preprocessor, which executes instructions before the actual compilation of the code begins. Here's an example that shows how to use #endif in a conditional compile block: Let's say you have a program where you want to complete different blocks depending on whether a macro is defined or not. To accomplish this we can use #if, #else and #endif.

  #include <stdio.h>
 // Define a macro for demonstration purposes
 #define SWITCH_ENABLED
 int main()
  {
 // Conditional compilation whether SWITCH_ENABLED is defined
 #ifdef SWITCH_ENABLED
 printf(" Switch Feature is enabled!\n");
 #else
 printf("The Switch Feature is not enabled.\n");
 #endif // End of the conditional block
 return 0;
 }

In this example:
• #ifdef SWITCH_ENABLED checks if SWITCH_ENABLED is defined.
• If it is already defined , then the code inside the #ifdef block (printf(“Switch Feature is enabled!\n”);) is compiled.
• If SWITCH_ENABLED is not defined, the compiler moves to the #else block and compiles that code instead.
• #endif ends the conditional compilation block.
This type of conditional compilation is useful for creating code that performs differently depending on certain compile-time conditions, such as whether a feature is enabled, the platform for which the code is being compiled, or for debugging purposes.

5. Conditional Compilation Directive #define Example in C
The #define directive in C language is a pre-processor command used to define macros. This macro can be used to control conditional compilation using directives like #if, #ifdef and #ifndef. When the pre-processor encounters a macro, it substitutes it with the resultant value or code throughout the program earlier starting the compilation process.
Following is an example that demonstrate the use of #define:

  #include <stdio.h>
 #define PI 3.14
 #define AREA(r) printf("Area of circle is: %f\n", PI * (r) * (r))
 int main() {
 float r = 3.0;
 // Using the defined macro PI
 printf("The Value of PI is: %.2f\n", PI);
 // Using the defined macro AREA
 AREA(r);
 return 0;
 }

In this example:
PI is defined as a macro with a value of 3.14. AREA is a macro function that accepts the radius argument and prints the area of the circle using the PI macro.
The value of PI directly shown using printf function.
AREA is used with the radius argument to calculate and print the area of the circle.
It shows how #define can be used to create macro functions and constants, make simpler code and making it more readable and manageable. However, since these are pre-processor directives written instructions, replacement are made before compilation, making the process more efficient.


6. Example #undef Conditional Compilation Directive in C
The #undef directive in C Language is used to undefine a previously defined macro. This can be especially useful in situations where you want to temporarily change the definition of a macro in a file or make sure that a macro is not defined at a particular place in the code. Here is an example demonstrating the use of #undef:

  #include <stdio.h>
 // Defining a macro initially
 #define SCREEN_LOCK_ON
 int main()
 {
  // Check if the macro is defined
  #ifdef SCREEN_LOCK_ON
  printf("The screen is initially on.\n");
  #else
   printf("The screen is initially off.\n");
  #endif
 // Undefine the macro
 #undef SCREEN_LOCK_ON
 // Check once again whether the macro is defined
 #ifdef SCREEN_LOCK_ON
  printf("The screen lock is still on.\n");
 #else
  printf("The Screnn lock is now off.\n");
 #endif
 return 0;
 }

In this code:
• Initially, SCREEN_LOCK_ON is defined using #define SCREEN_LOCK_ON.
• The first #ifdef SCREEN_LOCK_ON block checks if SCREEN_LOCK_ON is defined. Since it is, “The screen lock is still on.” is printed.
• The macro is then undefined with #undef SCREEN_LOCK_ON.
• After undefining, the second #ifdef SCREEN_LOCK_ON block checks again if SCREEN_LOCK_ON is defined. Since it’s undefined, “The Screnn lock is now off.” is printed.
The #undef directive is regularly used in complex projects where conditional compilation is requisite based on varying necessities or to avoid conflicts with macros defined in further included files.


Previous Topic:-->> File Inclusion in C. || Next topic:-->>Other Directives in C.