totn C Language

C Language: #if Directive

This C tutorial explains how to use the #if preprocessor directive in the C language.

Description

In the C Programming Language, the #if directive allows for conditional compilation. The preprocessor evaluates an expression provided with the #if directive to determine if the subsequent code should be included in the compilation process.

Syntax

The syntax for the #if directive in the C language is:

#if conditional_expression
conditional_expression
Expression that the preprocessor will evaluate to determine if the C source code that follows the #if directive will be included into the final compiled application.

Note

Example

The following example shows how to use the #if directive in the C language:

/* Example using #if directive by TechOnTheNet.com */

#include <stdio.h>

#define WINDOWS 1

int main()
{
   printf("TechOnTheNet is a great ");

   #if WINDOWS
   printf("Windows ");
   #endif

   printf("resource.\n");

   return 0;
}

Here is the output of the executable program:

TechOnTheNet is a great Windows resource.