PRB: C4127 Generated When Subexpression Evaluates to Constant

Last reviewed: July 17, 1997
Article ID: 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 <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
KBCategory: kbtool kbprb
KBSubcategory: CLIss
Keywords : kb16bitonly


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 17, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.