C Language: #else Directive
This C tutorial explains how to use the #else preprocessor directive in the C language.
Description
In the C Programming Language, the #else directive provides an alternate action when used with the #if, #ifdef, or #ifndef directives. The preprocessor will include the C source code that follows the #else statement when the condition for the #if, #ifdef, or #ifndef directive evaluates to false.
Syntax
The syntax for the #else directive in the C language is:
#else
Note
- The #else directive must be closed by an #endif directive.
Example
The following example shows how to use the #else directive in the C language:
/* Example using #else directive by TechOnTheNet.com */ #include <stdio.h> #define YEARS_OLD 12 int main() { #if YEARS_OLD < 10 printf("TechOnTheNet is a great resource.\n"); #else printf("TechOnTheNet is over %d years old.\n", YEARS_OLD); #endif return 0; }
Here is the output of the executable program:
TechOnTheNet is over 12 years old.
Advertisements