Skip to Main Content or Page Contents

C Programming Tutorial. - #if, #elif, #else, and #endif

C Programming - Conditional ifs

Results per page:

Match: any search words all search words

Page Contents

#ifdef and #ifndef

#if, #elif, #else, and #endif

#ifdef and #ifndef

Syntax
#ifdef identifier
#ifndef identifier

Description
The #ifdef and #ifndef conditional directives let you test whether an identifier is currently defined or not; that is, whether a previous #define command has been processed for that identifier and is still in force. The line
#ifdef identifier
has exactly the same effect as
#if 1
if identifier is currently defined, and the same effect as
#if 0
if identifier is currently undefined.
#ifndef tests true for the "not-defined" condition, so the line
#ifndef identifier
has exactly the same effect as
#if 0 if identifier is currently defined, and the same effect as
#if 1
if identifier is currently undefined.
The syntax thereafter follows that of the
#if, #elif, #else, and #endif.
An identifier defined as NULL is considered to be defined.

#if, #elif, #else, and #endif

Syntax


#if constant-expression-1
<section-1>
<#elif constant-expression-2 newline section-2>
     .
     .
     .
<#elif constant-expression-n newline section-n>
<#else  final-section>
#endif
#if constant-expression-1
<#elif constant-expression-2 newline section-2>
.
.
.
<#elif constant-expression-n newline section-n>
<#else final-section>
#endif
Description
Borland C++ supports conditional compilation by replacing the appropriate source-code lines with a blank line. The lines thus ignored are those beginning with # (except the #if, #ifdef, #ifndef, # else, #elif, and #endif directives), as well as any lines that are not to be compiled as a result of the directives. All conditional compilation directives must be completed in the source or include file in which they are begun.

The conditional directives #if, #elif, #else, and #endif work like the normal C conditional operators. If the constant-expression-1 (subject to macro expansion) evaluates to nonzero (true), the lines of code (possibly empty) represented by section-1, whether preprocessor command lines or normal source lines, are preprocessed and, as appropriate, passed to the Borland C++ compiler. Otherwise, if constant-expression-1 evaluates to zero (false), section-1 is ignored (no macro expansion and no compilation).

In the true case, after section-1 has been preprocessed, control passes to the matching #endif (which ends this conditional sequence) and continues with next-section. In the false case, control passes to the next #elif line (if any) where constant-expression-2 is evaluated. If true, section-2 is processed, after which control moves on to the matching #endif. Otherwise, if constant-expression-2 is false, control passes to the next #elif, and so on, until either #else or #endif is reached. The optional # else is used as an alternative condition for which all previous tests have proved false. The #endif ends the conditional sequence.

The processed section can contain further conditional clauses, nested to any depth; each #if must be carefully balanced with a closing #endif. The net result of the preceding scenario is that only one section (possibly empty) is passed on for further processing. The bypassed sections are relevant only for keeping track of any nested conditionals, so that each #if can be matched with its correct # endif. The constant expressions to be tested must evaluate to a constant integral value.

 

 

 


© tutorials4u.com
HTML Tutorial by John McGuinn.