You are here

Conditional compilation in C programming language

Conditional compilation in C programming language: Conditional compilation as the name implies that the code is compiled if certain condition(s) hold true. Normally we use if keyword for checking some condition so we have to use something different so that compiler can determine whether to compile the code or not. The different thing is #if.

Now consider the following code to quickly understand the scenario:

C program

#include <stdio.h>

#define x 10      

int main()
{
    #ifdef x
      printf("hello\n");      // this is compiled as  x is defined
   #else
      printf("bye\n");       // this isn't compiled
   #endif
   
   return 0;
}

Conditional compilation example in C language

#include <stdio.h>

int main()
{
  #define COMPUTER "An amazing device"

  #ifdef COMPUTER
    printf(COMPUTER);
  #endif

  return 0;
}