INFO: #define Must Evaluate to Integer Const for #if ExpressionLast reviewed: September 4, 1997Article ID: Q69934 |
The information in this article applies to:
SUMMARYWhen 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 INFORMATIONIf 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 expressionFor example, this error occurs with the following code fragment:
#define CONSTANT_NAME "YES" #if CONSTANT_NAME ... #endifBecause 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:
#define CONSTANT_NAME YES #if CONSTANT_NAME Code to use on YES... #elif CONSTANT_NAME==NO Code to use on NO... #endifIn this example, YES is undefined, so it evaluates to zero. Therefore, CONSTANT_NAME is also zero. Thus, the #if expression "CONSTANT_NAME" evaluates to false and the "Code to use on YES" 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 "CONSTANT_NAME==NO" evaluates to true (0 == 0). This causes the preprocessor NOT to remove the "Code to do on NO", 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 : CLngIss kbfasttip Version : MS-DOS:5.1,6.0,6.00a,6.00ax,7.0; OS/2:5.1,6.0,6.00a; WINDOWS:1.0,1.5; WINDOWS NT:1.0,2.0,4.0,5.0 Platform : MS-DOS NT OS/2 WINDOWS Issue type : kbinfo |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |