Conditional Compilation in C/C++ Programming !

Conditional Compilation is a very important feature of C/C++ programming languages. Conditional compilation allows the compiler to skip over a part of the source code by inserting the preprocessing commands #ifdef, #else and #endif. The general form of using this is as follows:

#ifdef macro_name

statement_1;

statement_2;

#else

statement_3;

statement_4;

#endif

The above syntax says that, if the macro_name has been #defined then,the block of statements between #ifdef and #else will be processed and compiled by the compiler after the pre-processing stage of the compilation process. It means assembly code would be generated for only those set of statements that are associated between #ifdef and #else Otherwise the block of statements between #else and #endif will be processed and compiled. Let us look into an example program.

int main( )

{

#ifdef EMBEDDED_SYSTEMS

printf( “Subhash Embedded Classes\n” );

printf( “Embedded Systems Training\n” );

#else

printf( “Subhash Programming Classes\n”);

printf( “Advance Java Programming\n” );

#endif

return 0;

}

Output: ( Note that, In the above program the macro EMBEDDED_SYSTEMS is not defined )

Subhash Programming Classes

Advance Java Programming

#define EMBEDDED_SYSTEMS 1

int main( )

{

#ifdef EMBEDDED_SYSTEMS

printf( “Subhash Embedded Classes\n” );

printf( “Embedded Systems Training\n” );

#else

printf( “Subhash Programming Classes\n”);

printf( “Advance Java Programming\n” );

#endif

return 0;

}

Output: ( Note that, In the above program the macro EMBEDDED_SYSTEMS is defined )

Subhash Embedded Classes

Embedded Systems Training

 

Author Information:

Subhash.K.U, Principal Mentor, Subhash Programming Classes,Bangalore.

VIEW SUBHASH’s PROFILE

SUBHASH LOVES TO TEACH ! CLICK HERE !

One Response Comment

  • A good note  February 23, 2016 at 3:43 pm

    Thanks for sharing this note

    Reply

Leave A Comment

Please enter your name. Please enter an valid email address. Please enter a message.