The C Preprocessor: If |
---|
Next: Defined, Previous: Ifdef, Up: Conditional Syntax [Contents][Index]
The ‘#if’ directive allows you to test the value of an arithmetic expression, rather than the mere existence of one macro. Its syntax is
#if expression controlled text #endif /* expression */
expression is a C expression of integer type, subject to stringent restrictions. It may contain
&&
and ||
). The latter two obey the usual
short-circuiting rules of standard C.
defined
operator, which lets you check whether macros
are defined in the middle of an ‘#if’.
#if MACRO
instead of
#ifdef MACRO
, if you know that MACRO, when defined, will
always have a nonzero value. Function-like macros used without their
function call parentheses are also treated as zero.
In some contexts this shortcut is undesirable. The -Wundef option causes GCC to warn whenever it encounters an identifier which is not a macro in an ‘#if’.
The preprocessor does not know anything about types in the language.
Therefore, sizeof
operators are not recognized in ‘#if’, and
neither are enum
constants. They will be taken as identifiers
which are not macros, and replaced by zero. In the case of
sizeof
, this is likely to cause the expression to be invalid.
The preprocessor calculates the value of expression. It carries out all calculations in the widest integer type known to the compiler; on most machines supported by GCC this is 64 bits. This is not the same rule as the compiler uses to calculate the value of a constant expression, and may give different results in some cases. If the value comes out to be nonzero, the ‘#if’ succeeds and the controlled text is included; otherwise it is skipped.
Next: Defined, Previous: Ifdef, Up: Conditional Syntax [Contents][Index]