A conditional inclusion statement or directive in C preprocessor resembles in some ways an if
statement in C, but there are certain differences to understand between them. The condition in an if
statement is tested during the execution of your program, while conditional inclusion statements are evaluated at compile time. Purpose of a conditional inclusion statement is to allow the program to behave differently from run to run, depending upon the data it is operating on. Conditional inclusion statements are evaluated at compile time or even before compilation by a special program called Preprocessor. Preprocessor is part of C compiler, which evaluates preprocessor directives in the final token stream passed to the compiler. By using conditional inclusion statements different code can be included in the program depending on the situation at the time of compilation.
Another great advantage offered by conditional inclusion statements (preprocessor directives) is 'once~only~include~files'. Very often, one header file includes another. It can easily result that a certain header file is included more than once. This may lead to errors, if the header file defines structure types or typedefs, and is certainly wasteful. Therefore, we often wish to prevent multiple inclusion of a header file. Conditional inclusion statements are used to prevent multiple inclusion of a header file.
The C preprocessor supports conditional compilation of parts of source file. C preprocessor directives control conditional inclusion. C programming language provides #if
, #else
, #elif
, #ifdef
, #ifndef
, and #endif
conditional preprocessor directives.
Following are three behavioural and important reasons to use conditional inclusion statements.
#if
, #else
, #elif
, #ifdef
, #ifndef
, and #endif
are the most commonly used conditional compilation directives. These directives allow us to conditionally include portions of code based upon the outcome of a constant expression. Syntax-wise each of the directives (if-line, else-line, elif-line, and #endif) appears alone on a line. Each of #if
, #elif
, #else
, #ifdef
, and #ifndef
directives control code block until first #elif
, #else
, #endif
directive not belonging to any inner conditional preprocessing blocks. The constant expressions in #if
and subsequent #elif
lines are evaluated in order until an expression with a non-zero value is found. Once a successful #if
or #elif
line has been found and its text processed, succeeding #elif
and #else
lines, together with their text, are discarded. If all the expressions are zero, and there is an #else
, the text following the #else
is treated normally.
Syntax Examples of conditional inclusion statements
/* Demonstrating syntax of conditional inclusion statements */ #include <stdio.h> #include <string.h> #define SIZE 100 #define US 0 #define ENGLAND 1 #define FRANCE 2 #define ACTIVE_COUNTRY US int main(void) { #if SIZE > 99 printf("For 100 plus elements\n"); #else printf("For less than 100 elements\n"); #endif #if ACTIVE_COUNTRY == US char currency[] = "dollar"; #elif ACTIVE_COUNTRY == ENGLAND char currency[] = "pound"; #else char currency[] = "franc"; #endif printf("Currency: %s\n", currency); #ifndef PI //#ifndef PI is equivalent to #if ! defined (PI) #define PI 3.14 #else printf("PI defined\n", currency); #endif #ifdef PI //#ifdef PI is equivalent to #if defined (PI) printf("PI defined\n", currency); #else #define PI 3.14 #endif } OUTPUT ====== For 100 plus elements Currency: dollar PI defined
Hope you have enjoyed reading the use of conditional inclusion statements in C. Conditional inclusion statements instruct the preprocessor to include a piece of code conditionally in the final token stream passed to the compiler. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!
Share this page on WhatsApp