A number of the directives control 1 conditional compilation, which allows certain portions of a program to be selectively compiled or ignored depending upon specified conditions. This C preprocessor feature is known as conditional compailation. The directives concerned are:
#if, #ifdef, #ifndef, #elif, #else, #endif together with the preprocessor unary operator defined.
For example, let us consider a macro is named as NAME.
#ifdef NAME
/* compile these lines if NAME is defined */
#endif
#ifndef NAME
/*compile these lines if NAME is not defined*/
#else
/*compile these lines if NAME is defined */
#endif
So, #ifdef and #endif can be used to test the definition or otherwise of a given macro name. Of course, the #else can be used with #ifdef too. There is no ambiguity about what a given #else binds to, because the use of #endif to delimit the scope of these directives eliminates any possible ambiguity.
These directives are most commonly used to select small fragments of C that are machine specific (when it is not possible to make the whole program completely machine independent), or some times to select different algorithms depending on the need to make trade-offs.
The #if and #elif constructs take a single integral constant expression as their arguments. The preprocessor integral constant expressions are the same as other integral constant expressions except that they must not contain cast operators. In this context, the expression defined name or defined(NAME) evaluates to 1 if NAME is currently defined , 0 if it is not. Any other identifiers in the expression including those that are C keywords are replaced with the value 0. Then the expression is evaluated . The replacement even of keywords means that sizeof cannot be used in these expressions to get the result that you would normally expect. As with the other conditional statements in C ,a resulting value of zero is used to represent false, anything else is true.
The preprocessor always use arithmetic with at least the ranges defined in the <limits.h> file and treats int expressions as long int and unsigned int as unsigned long int. character constants do not necessarily have the same values as they do at execution time, so for highly portable programs, it is best to avoid using them in preprocessor expressions. Overall, the rules mean that it is possible to get arithmetic results from the preprocessor which are different from the results at run time; although presumably only if the translation and execution are done on different machines.
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc