This blog is under construction

Sunday 3 June 2012

Conditional compilation in C language

Conditional compilation is a compiling method which helps to generate different source code or executable based on the conditions provided to the preprocessor directives(#ifdef, #ifndef, #else).  Basically, preprocessor would check the truth value of the conditional and modify the source code based on the result. Suppose, if the resultant value of the conditional is zero, then the code block owned by the evaluated conditional would be removed during preprocessing.

Consider the following example,

   #define CLIENT 1
   #define NUM1 10
   #define NUM2 20
   int main() {
        int a = NUM1, b = NUM2;
        #ifdef CLIENT
             printf("Client1's Addition O/P: %d\n", a + b);
        #else
             printf("Client2's Addition O/P: %d\n", a + b);
        #endif
        return 0;
   }

Here, the macro CLIENT has been defined and its values is 1.  So, the statements present between #ifdef and #else will remain after preprocessing.  Whereas, the code present between #else and #endif would be removed during preprocessing.  Below is the preprocessed code for the above program.

   # 1 "condcompile.c"
   # 1 ""
   # 1 ""
   # 1 "condcompile.c"

   int main() {
        int a = 10, b = 20;
        printf("Client1's Addition O/P: %d\n", a + b);
        return 0;
   }

From the above output, we could see that the following statement has been removed from code since #else evaluates to 0.
   printf("Client2's Addition O/P: %d\n", a + b);


Let us see another example for better understanding.
    #define CLIENT 1
    #define NUM1 10
    #define NUM2 20
    int main() {
           int a = NUM1, b = NUM2;
      #ifndef CLIENT
           printf("Client1's Addition O/P: %d\n", a + b);
      #else
           printf("Client2's Addition O/P: %d\n", a + b);
      #endif
           return 0;
   }

In the above program, we have changed #ifdef to #ifndef.  #ifndef checks whether the given macro is not defined.  In our case, #ifndef CLIENT would check whether the macro named CLIENT is defined or not.  In this case, macro CLIENT is defined.   So, the code block present in-between #else and #endif would remain after preprocession.  Whereas, the code block present in-between #ifndef and #else would be removed during preprocessing.

  /* preprocessing the source code */
  jp@jp-VirtualBox:~/$ gcc -E condcompile.c
  
  /* preprocessed code */
  # 1 "condcompile.c"
  # 1 ""
  # 1 ""
  # 1 "condcompile.c"
  int main() {
         int a = 10, b = 20;
         printf("Client2's Addition O/P: %d\n", a + b);
        //Client2's code alone got compiled
       return 0;
  }

Conditional compilation example in C

  #include <stdio.h>
  #define CLIENT 1
  #define NUM1 10
  #define NUM2 20
  int main() {
        int a = NUM1, b = NUM2;
        #ifdef CLIENT
             printf("Client1's Addition O/P: %d\n", a + b);
        #else
             printf("Client2's Addition O/P: %d\n", a + b);
        #endif
        return 0;
  }

  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Client1's Addition O/P: 30


1 comment:

  1. Dell Laptop Repair Center in Noida is no.1 service center which provides door to door services in or its nearby areas. We have expert, technicians who can repair your laptop at your home. . Call us: 9891868324

    ReplyDelete