Back to: C Tutorials For Beginners and Professionals
Conditional Compilation Directives in C
In this article, I am going to discuss the Conditional Compilation Directives in C with Examples. Please read our previous article, where we discussed File Inclusion Directives in C. At the end of this article, you will understand what Conditional Compilation Directives in C are and when and how to use Conditional Compilation Directives in C Program with examples.
Conditional Compilation Pre-Processor Directives in C:
In this technique, pre-processor depends on the conditional block name to be passed from the compilation process or not which is decided at the time of pre-processing. If the condition is true, then the block will be pass from the compilation process, if the condition is false then the complete block of the statements will be removed from the source at the time of the pre-processor.
The basic advantage of this pre-processor is reducing .exe file size because when source code is reduced then automatically object code is reduced, so exe file size also will be reduced. Let us see an example for understanding this concept
Program:
#include <stdio.h> void main () { printf ("A"); #if 5<8!=1 printf ("CTUTORIAL"); printf ("C"); #endif printf ("B"); }
Output: AB
In the above program at the time of pre-processing condition become false so automatically correspondent block will be removing from the source and it passes to compilation. So, by using conditional compilation pre-processor, we can reduce the size of the .exe file because at the time of pre-processing by removing the statements it reduces obj file size then automatically .exe file size reduced.
Program:
#include <stdio.h> void main () { printf ("CTUTORIAL"); #if 2>5!=2<5 printf ("A"); printf ("B"); #else printf ("C"); printf ("D"); #endif printf ("Welcome"); }
Output: CTUTORIALABWelcome
Program:
#include <stdio.h> void main () { printf ("CTUTORIAL"); #if 2>5!=0 printf ("A"); printf ("B"); #elif 5<0 printf ("C"); printf ("D"); #else printf ("Hi"); printf ("Bye"); #endif }
Output: CTUTORIALHiBye
#ifdef & #ifndef:
#ifdef & #ifndef are called Macro Testing Conditional Compilation Pre-Processor. When we are working with this pre-processor, depends on the condition only, code will pass for the compilation process (depends on macro status). By using this pre-processor, we can avoid multiple substitutions of the header file code.
Program using #ifdef:
#include <stdio.h> #define CTUTORIAL void main () { printf ("WELCOME"); #ifdef CTUTORIAL printf ("Hi"); printf ("Bye"); #endif }
Output: WELCOMEHiBye
Program using #ifndef:
#include <stdio.h> #define CTUTORIAL void main () { printf ("WELCOME"); #ifndef CTUTORIAL printf ("Hi"); printf ("Bye"); #endif }
Output: WELCOME
In the previous program, if CTUTORIAL macro is not defined then the corresponding block of code is not passing for the compilation process. CTUTORIAL is called null macro because it doesn’t have any replacement text.
Program:
#include <stdio.h> #define Test void main () { printf ("WELCOME"); #ifndef Test printf ("A"); printf ("B"); #endif printf ("Hello"); }
Output: WELCOMEHello
#undef:
By using this pre-processor, we can close the scope of an existing macro. Generally, this macro is required, when we are redefining an existing macro. After closing the scope of a macro, it is not possible to access it until it is redefined.
Program:
#include<stdio.h> #define A 11 void main () { printf ("%d", A); // A =22 Error because here A is constant, it is already replaced with 11 // #define A 22 Error because A is already defined with 11, we cannot do this #undef A // first undef, then def #define A 22 printf ("%d", A); #undef A #define A 33 printf ("%d", A); #undef A // printf("%d", A); Error }
Output: 112233
In the next article, I am going to discuss Miscellaneous Directives in C language. Here, in this article, I try to explain Conditional Compilation Directives in C. I hope you enjoy this Conditional Compilation Directive in C article. I would like to have your feedback. Please post your feedback, question, or comments about this article.