We have seen that we can check the existence of a macro by using #ifdef directive. However, there is another way to do the same. The alternative to #ifdef directive is to use the defined unary operator.
DEFINED
OPERATOR
We
have seen that we can check the existence of a macro by using #ifdef directive. However, there is
another way to do the same. The alternative to #ifdef directive is to use the defined unary operator. The defined
operator has one of the following forms:
defined MACRO
or
defined (MACRO)
The
above expression evaluates to 1 if MACRO
is defined and to o if it is not. The defined operator helps you to check for
macro definitions in one concise line without having to use many #ifdef or #ifndef directives. For example, consider the following macro
checks:
#ifdef MACRO1
#ifdef MACRO2
Controlled text1
#else
printf("\n MACROS not
defined");
#endif
OR
#if defined (MACRO1) &&
defined (MACRO2)
Controlled text1
#else
printf("\n MACROS not
defined");
#endif
As
evident from the above example, the defined operator can be combined in any
logical expression using the c logical operators. However, this operator can
only be used in the evaluated expression of an #if or #elif preprocessor
directive.
Programming in C: Unit I (d): Preprocessor Directives : Tag: : with Example C Programs | Preprocessor Directives - Defined Operator
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation