adsense


Friday, 27 November 2015

C++ LANGUAGE CONDITIONAL COMPILATION

COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button



C++ LANGUAGE CONDITIONAL COMPILATION




Conditional Compilation 

There are several directives, which can be used to compile 
selective portions of your program's source code. 
This process is called conditional compilation. 
The conditional preprocessor construct is much like 
the ‘if’ selection structure. Consider the following 
preprocessor code: 

#ifndef NULL 
   #define NULL 0 
#endif 

You can compile a program for debugging purpose. 
You can also turn on or off the debugging using a 
single macro as follows: 

#ifdef DEBUG 

   cerr <<"Variable x = " << x << endl; 
#endif 
This causes the cerr statement to be compiled in the program 
if the symbolic constant DEBUG has been defined before directive 
#ifdef DEBUG. You can use #if 0 statement to comment out 
a portion of the program as follows: 

#if 0 
   code prevented from compiling 
#endif 
Let us try the following example: 
#include <iostream> 
using namespace std; 
#define DEBUG 
 
#define MIN(a,b) (((a)<(b)) ? a : b) 
 
int main () 
{ 
   int i, j; 
   i = 100; 
   j = 30; 
#ifdef DEBUG 
   cerr <<"Trace: Inside main function" << endl; 
#endif 
 
#if 0 
   /* This is commented part */ 
   cout << MKSTR(HELLO C++) << endl; 
#endif 
 
   cout <<"The minimum is " << MIN(i, j) << endl; 
 
#ifdef DEBUG 
   cerr <<"Trace: Coming out of main function" << endl; 
#endif 
    return 0; 
} 
If we compile and run above code, this would produce the following result: 
Trace: Inside main function 
The minimum is 30 
Trace: Coming out of main function


No comments: