by Dinesh Thakur Category: Preprocessor Directives

The conditional directives are meant to control the compilation process. By using them we may conditionally include certain statements of the program for compilation. If the condition is not met the statements are not included. These directives also help us not to include duplicate files in the program and cause error. For example, see the following code:

#ifndef Size

#define Size 6

#else

#define Size 5

#endif

It directs that if Size has not been defined already, define it as 6. However, if it has been defined irrespective of the value then take it as 5. In Program, it has been defined before though value is not given, therefore, it takes the value after the else. The #ifndef must end with #endif. The conditional preprocessor directives starting with #if must end with #endif. Program presents a good example of this concept.

#include <stdio.h>

#define Size
#ifndef Size
#define Size 6
#else
#define Size 5
#endif
void main()
{
   int y = 4 , Product;
   Product = Size * y;
   printf("Product = %d\n", Product);
}

Conditional Directives in C



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.