12.18 – Usage of conditional compilation

by subbu on December 31, 2013

In previous session we have learned how source code can be send conditionally to the compiler. Now we will see, in practical where do we use conditional compilation. Conditional compilation is generally used in 3 cases

To experiment with the code:

Some times to satisfy the client, programmer may need to comment a part of source code, which he feels outdated in view of client. It involves rewriting a part of source code and deleting the old one.  But the experienced programmers know that client may want old code back as it was. Rather retyping the old code again it is a better practice to comment the old code using conditional compilation statements and getting back whenever we need the old code back. Some people may argue that, we can put the old code in /*    */ instead of using conditional compilation statements. But we might have already written comments that we are about to comment out. This would end up comments within comments (nested comments)

#include<stdio.h>
#define NEW
int main()
{
 ------------
 ------------
 #ifndef NEW
 --------------
 -------------- // old code
 --------------
 #else
 --------------
 -------------- // new code
 --------------
 #endif
 --------------
 --------------
}

In the above outlined example, old code is executed if the macro NEW is not defined with #define statement, new code is executed if the macro NEW is defined

To develop portable applications:

We know that, executable code generated for one architecture may not work on another architecture (platform dependency). Say for example, an organization has two different types of computers and we are expected to write a program that works on both the computers. We can do so by writing the code for both platforms and selecting proper code while compiling using conditional compilation statements.

#include<stdio.h>
#define INTEL
int main()
{
 ------------
 ------------   //common code
 ------------
 #ifdef INTEL
 --------------
 -------------- // code for INTEL
 --------------
 #else
 --------------
 -------------- // code for Motorola
 --------------
 #endif
 --------------
 -------------- // common code
 --------------
}

Here if the macro INTEL is defined then the common code and suitable code for INTEL would be send to the compiler. If the macro INTEL is not defined then common code and suitable code for Motorola would be send to the compiler

Header guards:

We know that, a header file can include another header file. Some times it may be possible to include the same header file for multiple times in different paths. It may result compiler error. header guard In the above example util.h is included into other header files arith.h and logic.h, both these header files are included into program main.c. Totally functions belongs to util.h are included into main.c for two times that are through arith.h and logic.h. To prevent this from happening, we use header guard using conditional compilation statement We use the following format whenever we define a function prototype or a function definition in a header file.

#ifndef ARI
 #define ARI
 void adding(int x,int y);
 void sub(int x,int y);
#endif

When above header is included, the first thing it does is check whether ARI has been previous defined. If this is the first time we’ve included the header, ARI will not have been defined. Consequently, it #defines ARI and includes the contents of the file. If this is not the first time we’ve included the header, ARI will already have been defined from the first time the contents of the header were included. Consequently, the entire header will be ignored.

/* arith.h */
int sum(int x,int y)
{
 return x+y;
}
int sub(int x,int y)
{
 return x-y;
}

the above header file has no header guard, now we will see what happens when we include the header for multiple times

/* main.c */
#include "arith.h"
#include "arith.h"
int main()
{
 printf("Sum %d\n",sum(20,10));
 printf("Subtraction %d",sub(20,10));
 return 0;
}

Output in Turbo C: Error arith.h 2: Multiple declaration for ‘sum’ Error arith.h 6: Multiple declaration for ‘sub’ Output in Linux gcc: In file included from main.c:3: arith.h:1: error : redefinition of ‘sum’ arith.h:1: note: previous definition of ‘sum’ was here arith.h:5: error : redefinition of ‘sub’ arith.h:5: note: previous definition of ‘sub’ was here It can be rectified by using the header guard in arith.h. The header file with header guard is

 #ifndef ARI
 #define ARI
 /* arith.h */
 int sum(int x,int y)
 {
 return x+y;
 }
 int sub(int x,int y)
 {
 return x-y;
 }
 #endif

Previous post:

Next post: