Using defined. McGraw-Hill The Complete Reference Fourth Edition
تبلیغات
تبلیغات
C h a p t e r 1 0 : T h e P r e p r o c e s s o r a n d C o m m e n t s
247
For example,
#define LEN 100
#define WIDTH 100 char array[LEN][WIDTH];
#undef LEN
#undef WIDTH
/* at this point both LEN and WIDTH are undefined */
Both LEN and WIDTH are defined until the #undef statements are encountered.
#undef
is used principally to allow macro names to be localized to only those sections of code that need them.
Using defined
In addition to #ifdef, there is a second way to determine if a macro name is defined.
You can use the #if directive in conjunction with the defined compile-time operator.
The defined operator has this general form: defined macro-name
If macro-name is currently defined, then the expression is true. Otherwise, it is false. For example, to determine if the macro MYFILE is defined, you can use either of these two preprocessing commands:
#if defined MYFILE or
#ifdef MYFILE
You may also precede defined with the ! to reverse the condition. For example, the following fragment is compiled only if DEBUG is not defined.
#if !defined DEBUG printf("Final version!\n");
#endif
دانلود
تبلیغات