#if, #elif, #ifndef, #ifdef in C (Conditional Inclusion)

By: Lakshmi Emailed: 1646 times Printed: 2117 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

It is possible to control preprocessing itself with conditional statements that are evaluated during preprocessing. This provides a way to include code selectively, depending on the value of conditions evaluated during compilation.

The #if line evaluates a constant integer expression (which may not include sizeof, casts, or enum constants). If the expression is non-zero, subsequent lines until an #endif or #elif or #else are included. (The preprocessor statement #elif is like else-if.) The expression defined(name) in a #if is 1 if the name has been defined, and 0 otherwise.

For example, to make sure that the contents of a file hdr.h are included only once, the contents of the file are surrounded with a conditional like this:

   #if !defined(HDR)
   #define HDR

   /* contents of hdr.h go here */

   #endif
The first inclusion of hdr.h defines the name HDR; subsequent inclusions will find the name defined and skip down to the #endif. A similar style can be used to avoid including files multiple times. If this style is used consistently, then each header can itself include any other headers on which it depends, without the user of the header having to deal with the interdependence.

This sequence tests the name SYSTEM to decide which version of a header to include:

   #if SYSTEM == SYSV
       #define HDR "sysv.h"
   #elif SYSTEM == BSD
       #define HDR "bsd.h"
   #elif SYSTEM == MSDOS
       #define HDR "msdos.h"
   #else
       #define HDR "default.h"
   #endif
   #include HDR
The #ifdef and #ifndef lines are specialized forms that test whether a name is defined. The first example of #if above could have been written
   #ifndef HDR
   #define HDR

   /* contents of hdr.h go here */

   #endif

C Home | All C Tutorials | Latest C Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Lakshmi
Exporting a Certificate Authenticating Your Public Key example using keytool in Java
while and do-while example in Java
Common SQL Commands
Data Access Technologies in Java
perror() Function - example program in C
Struts-GUI and Struts Console
Variable-length Argument Lists sample program in C
#if, #elif, #ifndef, #ifdef in C (Conditional Inclusion)
Use ImageButtonBean for Image based Form Submits in Struts
break and continue loops in C
Arguments - Call by Value in C
Using command-line arguments in C++
How to handle Exceptions in C++
Gauge sample program in J2ME
What is Reflection? - getConstructors(), getFields(), and getMethods()

More Tutorials in C
Sum of the elements of an array in C
Printing a simple histogram in C
Sorting an integer array in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Command-line arguments in C
Calculator in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C
Unicode and UTF-8 in C
Formatting with printf in C
if, if...else and switch statements in C with samples
Statements in C

More Latest News
Most Viewed Articles (in C )
Using memset(), memcpy(), and memmove() in C
UNIX read and write system calls sample program in C
Printing a simple histogram in C
lseek() sample program in C
perror() Function - example program in C
Open, Creat, Close, Unlink system calls sample program in C
Find square and square root for a given number in C
Listing Files and Directories sample program in C
Using free() Function in C
goto and labels in C
Character Arrays in C
getch and ungetch in C
File Inclusion in C
Macro Substitution using #define in C
Structures and Functions in C
Most Emailed Articles (in C)
Multi-dimensional Arrays in C (Explained using date conversion program)
Arguments - Call by Value in C
Macro Substitution using #define in C
Formatting with printf in C
Unicode and UTF-8 in C
Character Arrays in C
Pointer Arrays and Pointers to Pointers in C
Open, Creat, Close, Unlink system calls sample program in C
lseek() sample program in C
Sum of the elements of an array in C
The for statement in C
Symbolic Constants using #define in C
Initialization of Variables in C
Pointers vs. Multi-dimensional Arrays in C
Using Bit-field in C