12.17 – Conditional compilation in C

by subbu on December 30, 2013

Conditional compilation in C language

As the name implies, it is the process of selecting the source code conditionally from the program and sending to the compiler using preprocessor statements like #if, #ifdef, #ifndef, #else, #elif, #endif.

#if preprocessor conditional statement:

#if conditional compilation statement

Here the preprocessor selection statement #if checks the condition. If it is true or a non-zero then statements between #if and #else are sent to the compiler. If it is false or a zero then statements between #else and #endif are sent to the compiler.

Note: Here the expression must give an integer constant. sizeof(), casts and enums can’t be used.

Now we will see the fact through an example

/* main.c */
#include <stdio.h>
int main()
{
int a,b;
printf("Enter two numbers:\n");
scanf("%d%d",&a,&b);
#if 5>10
 printf("Sum %d",a+b);
#else
 if(a==b)
  printf("Eqauls");
 else if(a>b)
  printf("Biggest number %d",a);
 else
  printf("Biggest number %d",b);
#endif
return 0;
}

Execution:
Enter two numbers:
45
30
Biggest number 45

In the example as 5>10 gives an integer constant 0 (false), the preprocessor sends the code between #else and #endif to the compiler. Now we can realize this by looking at it’s expanded source

/* main.i */
main.c 2: int main()
main.c 3: {
main.c 4: int a,b;
main.c 5: printf("Enter two numbers:\n");
main.c 6: scanf("%d%d",&a,&b);
main.c 7:
main.c 8:
main.c 9:
main.c 10: if(a==b)
main.c 11: printf("Eqauls");
main.c 12: else if(a>b)
main.c 13: printf("Biggest number %d",a);
main.c 14: else
main.c 15: printf("Biggest number %d",b);
main.c 16:
main.c 17: return 0;
main.c 18: }

What would be output of following program?

#include <stdio.h>
#define A 10
#define B 40
int main()
{
#if A==B
 printf("Hello");
#elif A>B
 printf("World");
#else
 printf("CodingFox");
#endif
return 0;
}
Show Answer
Output:
CodingFox

What would be the output of following program?

#include <stdio.h>
#define X 45
int main()
{
#if !X
 printf("Hello");
#else
 printf("World");
#endif
return 0;
}
Show Answer
Output:
World

Here !45 is false, hence “printf(“World”);” is sent to the compiler

What would be the output of following program?

#include <stdio.h>
#define X 45.25
int main()
{
#if !X
 printf("Hello");
#else
 printf("World");
#endif
return 0;
}
Show Answer
Output:
main.c: In function ‘main’:
main.c:2:11: error: floating constant in preprocessor expression
#define X 45.25
^
main.c:5:10: note: in expansion of macro ‘X’
#if !X
^
Preprocessor expression must give an integer constant

What would be the output of following program?

#include <stdio.h>
int main()
{
#if sizeof(short)==2
 printf("Hello");
#else
 printf("World");
#endif
return 0;
}
Show Answer
Output:
main.c: In function ‘main’:
main.c:4:15: error: missing binary operator before token “(”
#if sizeof(short)==2
^
sizeof(), cast and enum types can’t be used as preprocessor conditional expression

defined() preprocessor checker:

It checks whether a macro with the name is defined or not. It returns true if in case macro with the name is defined otherwise returns false

#include <stdio.h>
#define MAX
int main()
{
#if defined(MAX)
 printf("Hello");
#else
 printf("World");
#endif
 printf("\n");
#if !defined(MIN)
 printf("Fox");
#else
 printf("Duck");
#endif
return 0;
}

Output:
Hello
Fox

Here defined(MAX) gives true as the macro MAX is defined, defined(MIN) gives false as the macro MIN is not defined

#ifdef, #ifndef preprocessor conditional statements:

We can use #ifdef in place of #if defined(), #ifndef in place of #if !defined().

#ifdef conditional compilation statement

Here if the MACRO is defined with #define statement within the scope then code between #ifdef and #else is send to the compiler otherwise statements between #else and #endif are send to the compiler

#ifndef conditional compilation statement

Here statements between #ifndef and #else are send to the compiler if the MACRO is not defined, statements between #else and #endif are send to the compiler if MACRO is defined within the scope.
In simple it is just opposite to #ifdef

#include <stdio.h>
#define MAX
int main()
{
#ifdef MAX
 printf("Hello");
#else
 printf("World");
#endif
 printf("\n");
#ifndef MIN
 printf("Fox");
#else
 printf("Duck");
#endif
return 0;
}

Output:
Hello
Fox

In the above example as the macro MAX is defined “printf(“Hello”);” is sent to the compiler.
As the macro MIN is not defined “printf(“Fox”);” is sent to the compiler

Previous post:

Next post: