
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is Conditional Compilation in C Language
In C programming language, several directives control the selective compilation of different parts of the program code. Conditional Compilation is a set of preprocessing directives that allows us to exclude and include parts of a program based on specified conditions. Each directive in the compilation is processed across different platforms, including debug and release versions. They are as follows ?
- #if
- #else
- #elif
- #endif
The "#if" Directory
The #if is a directive preprocessor that evaluates the expression or condition. If the condition is true then this directory executes the code otherwise #elseif or #endif code is executed.
The general form of #if is as follows ?
#if constant_expression statement sequence #endif
The #else statement defines a block of code that runs when the condition in an if statement is false. This works similarly to the other keyword in C.
#elif stands for "else if" and creates an if-else-if compilation chain. The #elif statement is used within an #if statement. This includes a section of text that is determined if its condition is True. Among other users, #if provides an alternative "commenting out" code method.
For example, this code snippet is a preprocessor directive that comments out the printf statement, this disables it during the compilation.
#if 0 printf("#d", total); #endif
Here, the compiler will ignore printf("#d", total);
Example
This C program checks if NUMBER is equal to 0 using the #if directive. If true, it prints "Value of Number is:0" and then waits for user input getchar() before generating.
#include <stdio.h> #define NUMBER 0 void main() { #if (NUMBER == 0) printf("Value of Number is: %d", NUMBER); #endif getchar(); }
Output
The result is obtained as follows ?
Value of Number is: 0
The "#else" Directory
The #else preprocessor determines the expression on condition if condition of #if is false. This can be used with #elif, #ifndef directive.
Syntax
Following is the syntax of #else directive ?
#else //else code
Example
In the following example, we print "Value of Number is non-zero" because NUMBER is set to 1. This C program checks if NUMBER is equal to 0. If true, it prints the value of NUMBER. Otherwise, it prints "Value of Number is non-zero" and waits for the user.
#include <stdio.h> #define NUMBER 1 void main() { #if NUMBER == 0 printf("Value of Number is: %d", NUMBER); #else printf("Value of Number is non-zero"); #endif getchar(); }
Output
We will get the result as follows ?
Value of Number is non-zero
The "elif" Directory
The #elif directive is a unique action when it is combined with the other directives. This condition specifies after #if is false, the processor will specify the condition after #elif. The code will be blocked after #elif is compiled if the corresponding test is true.
Syntax
Following is the syntax of the #elif directive ?
#elif condition_expression
Example
In this example, we print "Suresh is good boy" because YEARS_OLD is 15, which is less than or equal to 20. This C program checks if YEARS_OLD is 15 and prints "Suresh is a good boy".
#include <stdio.h> #define YEARS_OLD 15 int main() { #if YEARS_OLD <= 20 printf("Suresh is a good boy.
"); #elif YEARS_OLD > 20 printf("Suresh is %d years old.
", YEARS_OLD); #endif return 0; }
Output
The result is produced as follows ?
Suresh is a good boy.
The "#endif" Directory
In the following #endif directive specifies the end of the #if, #ifdef, or #ifndef. When the #endif directive is encountered, it completes the preprocessing of the determined opening directive.
Syntax
Following is the syntax of the #endif ?
#endif
Example
In this example we print "TutorialsPoint is great Learning Platform" because WINDOWS is defined as 1, this enables the conditional code.
#include <stdio.h> #define WINDOWS 1 int main() { printf("TutorialsPoint is a great"); #if WINDOWS printf("Learning "); #endif printf("Platform.
"); return 0; }
Output
We will produce the following result ?
TutorialsPoint is a greatLearning Platform.
Basic Usage
Here is a C program demonstrating the use of #ifdef, #ifndef, #else, and #endif. This uses conditional compilation to check if a is defined. If a is specified, it prints "Hello I am here." Otherwise, it prints "Not defined".
#include <stdio.h> #define a 10 void main() { #ifdef a printf("Hello I am here.."); #endif #ifndef a printf("Not defined "); #else printf("R u There "); #endif }
Output
When the above program is executed, it produces the following result ?
Hello I am here.. R u There