Microsoft KB Archive/60734

{|
 * width="100%"|

PRB: C4127 Generated When Subexpression Evaluates to Constant

 * }

Q60734

6.00 6.00a 6.00ax 7.00 | 6.00 6.00a | 1.00 1.50 1.51 MS-DOS | OS/2 | WINDOWS kbtool kbprb -- The information in this article applies to: - The Microsoft C/C++ Compiler (CL.EXE), included with: - Microsoft C for MS-DOS, versions 6.0, 6.0a, and 6.0ax - Microsoft C for OS/2, versions 6.0 and 6.0a - Microsoft C/C++ for MS-DOS, versions 7.0 - Microsoft Visual C++ for Windows, versions 1.0, 1.5, and 1.51 -- SYMPTOMS ======== The following warning was added beginning with Microsoft C version 6.0: C4127: conditional expression is constant Microsoft C/C++ version 7.0 and later use the following warning: C4727: conditional expression is constant This 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. CAUSE ===== The warning is strictly informational and does not necessarily indicate any problems in the code. MORE INFORMATION ================ In 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 >= 0 ALWAYS 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 void main(void) { unsigned hours; scanf ( &quot;%ud&quot;, &hours ); if ( hours >= 0 && hours <= 24 ) printf(&quot;Hours OK\n&quot;); else printf(&quot;Hours BAD\n&quot;); } Making a simple change, such as replacing the &quot;>=&quot; with a &quot;>&quot;, 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 KBCategory: kbtool kbprb KBSubcategory: CLIss

Keywords : kb16bitonly

Issue type :

Technology : kbVCsearch kbAudDeveloper kbCVCComp