Conditional Compilation in C

This C Tutorial explains Conditional Compilation in C programming.

Well! We all know that while debugging the source code of a program, we generally include printf() statements at several places of doubts to know until where has been execution going correct, until where have been if the values of required variables evaluated correctly? These statements we rather would not physically remove from the source code as the same might be required again while maintenance modifications of the program. In such situations, conditional compilation
is the perfect! Let’s consider a simple ex.,

#include <stdio.h>
#define DEBUG printf("value of x = %d and y = %d.\n", x, y)
 
void increase(int, int);
int main(void)
{
    int x = 5, y = 6;
 
    x++;
    y++;
    DEBUG;
    increase(++x, y++);
    DEBUG;
 
    ++x;
    ++y;
    DEBUG;
}
 
void increase(int x, int y)
{
    DEBUG;
    x++;
    y++;
    DEBUG;
}

Notice that we inserted DEBUG statements at several places to know the modified values of variables x and y to ascertain the way how are they being evaluated in the program? But, of course, we wouldn’t like them to appear in the output once the evaluation trend is clear. we rather hide them by enclosing in conditional directives as follows,

    #if 0
        DEBUG;
    #endif

Notice the #if construct which had its matching #endif. This is the simplest conditional compilation construct. Constant exp. zero ‘0’ following #if is considered false and therefore preprocessor deleted the entire #if construct from its output while they are present in the source code.

advertisement
advertisement

#if construct also has optional #elif and #else constructs. There can be used any no. of #elif constructs. Let’s consider their syntax first,

#if   constan_exp
       /* statements */
#elif constant-exp
      /* other statements */
#else
      /* othet statements */

Notice that constant-exp must be a #defined symbol or literal constant! Variables that don’t attain their values until run time are not legal candidates because their values can’t be determined at the compile time. Let’s see another ex.,

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
#define MOUSE 10
#define CAT 0
 
#if MOUSE
    #include "mouse.h"
#elif CAT
    #include "cat.h"
#else
   #include "horse.h"
#endif
 
int main(void)
{
    int x = 10;
 
    printf("value of x is %d\n", x);
    return 0;
}

Observe in above program, how conditional compilation directives cause compiler what fragment of code to compile and what to skip. Also, note that any constant exp. is evaluated only if all previous ones are false. If none of the constant exp. is true, and else clause is present, it’s then executed. Preprocessor simply deletes those claues, constant exp. for which is false.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

advertisement
If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.