PRB: C4127 Generated When Subexpression Evaluates to ConstantLast reviewed: July 17, 1997Article ID: Q60734 |
6.00 6.00a 6.00ax 7.00 | 6.00 6.00a | 1.00 1.50 1.51
MS-DOS | OS/2 | WINDOWSkbtool kbprb
The information in this article applies to:
SYMPTOMSThe following warning was added beginning with Microsoft C version 6.0:
C4127: conditional expression is constantMicrosoft C/C++ version 7.0 and later use the following warning:
C4727: conditional expression is constantThis warning is designed to inform you that the controlling expression of an if statement or while loop evaluates to a constant, so the body of the loop is ALWAYS executed or NEVER executed. The warning may appear in certain expressions that don't seem to be constants, but this is because the compiler will generate this warning if ANY subexpression in a larger conditional expression evaluates to a constant.
CAUSEThe warning is strictly informational and does not necessarily indicate any problems in the code.
MORE INFORMATIONIn the sample code below, warning C4127 is generated by the optimizing compiler supplied with C version 6.0 if the code is compiled at warning level three or four (/W3 or /W4). The quick compiler (/qc) supplied with C version 6.0 does not generate the warning because it does not check for this situation. The fast compiler, invoked with the /f switch, supplied with C/C++ version 7.0 and Visual C/C++ versions 1.0 and 1.5 does not generate the C4727 warning at any warning level. The compilers included with Visual C++ 32-bit edition do not generate any warnings either. The optimizing compiler, invoked with the /f- switch, supplied with C/C++ version 7.0 and Visual C++ for Windows generates the C4727 warning for the sample code below only at warning level three or four. The optimizing compiler supplied with Visual C/C++ versions 1.0 and 1.5, also invoked with the /f- switch, generates the C4727 warning only at warning level four. The following expression
( hours >= 0 && hours <= 24 )is NOT a constant because hours could be EITHER in the range 0 (zero) to 24, or out of that range. However, this expression generates warning C4127 because the left subexpression
hours >= 0ALWAYS evaluates to true since hours is unsigned and an unsigned int is ALWAYS greater than or equal to zero. The compiler generates the warning to inform you of this situation.
Sample Code
/* Compile options needed: none */ #include <stdio.h> void main(void){ unsigned hours; scanf ( "%ud", &hours ); if ( hours >= 0 && hours <= 24 ) printf("Hours OK\n"); else printf("Hours BAD\n"); }Making a simple change, such as replacing the ">=" with a ">", eliminates the warning because the left expression can now evaluate to either true or false (for example, false if hours = 0; true otherwise).
|
Additional reference words: 7.00 8.00 8.00c 1.00 1.50 1.51
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |