What is the use of conditional inclusion statements in C?

What are Conditional Inclusion Statements?

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.

Why Conditional Inclusion Statements are Used?

Following are three behavioural and important reasons to use conditional inclusion statements.

  • A program may need to use different code depending on the machine or operating system it is to run on. In some cases the code for one operating system may be erroneous on another operating system; for example, it might refer to library routines that do not exist on the other system. When this happens, it is not enough to avoid executing the invalid code: merely having it in the program makes it impossible to link the program and run it. With a preprocessing conditional, the offending code can be effectively excised from the program when it is not valid.
  • You may want to be able to compile the same source file into two different programs. Sometimes the difference between the programs is that one makes frequent time-consuming consistency checks on its intermediate data, or prints the values of those data for debugging, while the other does not.
  • A conditional whose condition is always false is a good way to exclude code from the program but keep it as a sort of comment for future reference.

Syntax of 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
int main(void)
  #if SIZE > 99
    printf("For 100 plus elements\n");
    printf("For less than 100 elements\n");
    char currency[] = "dollar";
    char currency[] = "pound";
    char currency[] = "franc";
  printf("Currency: %s\n", currency);
  #ifndef PI //#ifndef PI is equivalent to #if ! defined (PI)
    #define PI 3.14
    printf("PI defined\n", currency);
  #ifdef PI //#ifdef PI is equivalent to #if defined (PI)
    printf("PI defined\n", currency);
    #define PI 3.14
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!

Get Free Tutorials by Email

About the Author

is the main author for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.

Today's Tech News

AlphaBay and Hansa dark web markets shut downPosted on Thursday July 20, 2017

The AlphaBay and Hansa marketplaces were known for trade in drugs, weapons and malware.

Kodi magazine 'directs readers to pirate content'Posted on Thursday July 20, 2017

The guide is being investigated by the Federation Against Copyright Theft.

Bitcoin swings as civil war loomsPosted on Thursday July 20, 2017

The Bitcoin community must agree how to tackle a slowdown in transaction times to avoid a schism.

Courtesy BBC News


Advertisements help running this site for free.

To view the content please disable AdBlocker and refresh the page.