Microsoft KB Archive/69934

= INFO: #define Must Evaluate to Integer Const for #if Expression =

Article ID: 69934

Article Last Modified on 12/12/2003

-

APPLIES TO


 * Microsoft Visual C++ 1.0 Professional Edition
 * Microsoft Visual C++ 1.5 Professional Edition
 * Microsoft Visual C++ 2.0 Professional Edition
 * Microsoft Visual C++ 4.0 Standard Edition
 * Microsoft Visual C++ 5.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 5.0 Professional Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition

-



This article was previously published under Q69934





SUMMARY
When you use the #define statement to define a constant, its value must evaluate to an integer constant if you are going to use it as the expression in an #if, #elif, or #else compiler directive. In addition, for any undefined constant that you use, the preprocessor assumes a value of zero.

If you are not sure how the compiler is handling certain directives, you can compile the code with one of the preprocessor options (/P /E /EP). This will allow you to see the exact effect of the preprocessor directives on the code.



MORE INFORMATION
If a #define'd constant that does not evaluate to an integer constant is used with the #if, #elif, or #else compiler directive, the following error message appears:

fatal error C1017: invalid integer constant expression

> For example, this error occurs with the following code fragment: #define CONSTANT_NAME &quot;YES&quot; #if CONSTANT_NAME ...  #endif

Because CONSTANT_NAME evaluates to a string constant and not an integer constant, the #if directive generates a C1017 error and halts the compiler.

The other notable situation is that any constant that is undefined is evaluated by the preprocessor to be zero. If an undefined constant is used inadvertently, unintended behavior may result. For example, in the following code fragment, the result is the reverse of what is actually intended:

Code to use on YES...  Code to use on NO...
 * 1) define CONSTANT_NAME YES
 * 2) if CONSTANT_NAME
 * 1) elif CONSTANT_NAME==NO
 * 1) endif

In this example, YES is undefined, so it evaluates to zero. Therefore, CONSTANT_NAME is also zero. Thus, the #if expression &quot;CONSTANT_NAME&quot; evaluates to false and the &quot;Code to use on YES&quot; is removed by the preprocessor, which is clearly NOT the intended behavior. Furthermore, because NO is also undefined, it also evaluates to zero, which means that the #elif expression &quot;CONSTANT_NAME==NO&quot; evaluates to true (0 == 0). This causes the preprocessor NOT to remove the &quot;Code to do on NO&quot;, which is also NOT the intended behavior.

By compiling with one of the preprocessor options (/P /E /EP), you can observe the preprocessor output in order to see exactly how the preprocessor is interpreting all directives. For more information on these options, consult the documentation or online help that accompanies your particular version of the compiler.

Keywords: kbinfo kblangc KB69934

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.