PRB: C4127 Generated When Subexpression Evaluates to Constant

ID Number: Q60734

6.00 6.00a 6.00ax | 6.00 6.00a

MS-DOS | OS/2

Summary:

SYMPTOMS

Microsoft C Compiler version 6.0 has a new compiler warning:

C4127: conditional expression is constant

Microsoft C/C++ version 7.0 has 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.

STATUS

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 if the code is

compiled at warning level three or four (/W3 or /W4). 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.

Note that this warning is generated only by the full optimizing

compiler because the quick compiler (/qc) does not check for this

situation.

Sample Code

-----------

#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: 6.00 6.00a 6.00ax