Compiler Warning (level 4) C4127
Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

Compiler Warning (level 4) C4127

Error Message

conditional expression is constant

The controlling expression of an if statement or while loop evaluates to a constant. If the controlling expression of a while loop is a constant because the loop will exit in the middle, consider replacing the while loop with a for loop. You can omit the initialization, termination test and loop increment of a for loop, which causes the loop to be infinite (like while(1)) and you can exit the loop from the body of the for statement.

The following sample generates C4127:

// C4127.cpp
// compile with: /W4
#include <stdio.h>
int main() {
   if (1 == 1) {}   // C4127
   while (1) { break; }   // C4127

   // OK
   for ( ; ; ) {
© 2015 Microsoft