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

Robotic barman pours Rory a pintPosted on Friday March 24, 2017

The BBC’s technology correspondent Rory Cellan-Jones gets a beer poured for him by a robotic barman - but how long does it take?

Row over US ISP customer data salesPosted on Friday March 24, 2017

US politicians have voted to let ISPs gather and sell data about what Americans do online.

Ofcom plans instant payback for broadband woesPosted on Friday March 24, 2017

Customers who suffer poor service could get automatic payouts under Ofcom's plan.

Courtesy BBC News