Categories
Preprocessor and Macros

Conditional Compilation

In C programming, you can instruct preprocessor whether to include a block of code or not. To do so, conditional directives can be used.

It’s similar to a if statement with one major difference.

The if statement is tested during the execution time to check whether a block of code should be executed or not whereas, the conditionals are used to include (or skip) a block of code in your program before execution.

Uses of Conditional

  • use different code depending on the machine, operating system
  • compile same source file in two different programs
  • to exclude certain code from the program but to keep it as reference for future purpose

How to use conditional?

To use conditional, #ifdef#if#defined#else and #elif directives are used.

#ifdef Directive

#ifdef MACRO     
   // conditional codes
#endif

Here, the conditional codes are included in the program only if MACRO is defined.

#if, #elif and #else Directive

#if expression
   // conditional codes
#endif

Here, expression is an expression of integer type (can be integers, characters, arithmetic expression, macros and so on).

The conditional codes are included in the program only if the expression is evaluated to a non-zero value.

The optional #else directive can be used with #if directive.

#if expression
   conditional codes if expression is non-zero
#else
   conditional if expression is 0
#endif

You can also add nested conditional to your #if...#else using #elif

#if expression
    // conditional codes if expression is non-zero
#elif expression1
    // conditional codes if expression is non-zero
#elif expression2
    // conditional codes if expression is non-zero
#else
    // conditional if all expressions are 0
#endif

#defined

The special operator #defined is used to test whether a certain macro is defined or not. It’s often used with #if directive.

#if defined BUFFER_SIZE && BUFFER_SIZE >= 2048
  // codes

Predefined Macros

Here are some predefined macros in C programming.

MacroValue
__DATE__A string containing the current date
__FILE__A string containing the file name
__LINE__An integer representing the current line number
__STDC__If follows ANSI standard C, then the value is a nonzero integer
__TIME__A string containing the current date.

Example 3: Get current time using __TIME__

The following program outputs the current time using __TIME__ macro.

#include <stdio.h>
int main()
{
   printf("Current time: %s",__TIME__);   
}

Output

Current time: 19:54:39

Leave a Reply

Your email address will not be published. Required fields are marked *