Questions tagged [c4127]

A warning generated at warning level 4 in visual-c++ compilers.

Microsoft Visual C++ compiler warning conditional expression is constant. Steps to produce:

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

   // OK
   for ( ; ; ) {
      printf("test\n");
      break;
   }
}
7 questions
20
votes
6 answers

C4127: Conditional Expression is Constant

The following code generates warning C4127 (conditional expression is constant) in Visual Studio 2010 (where alias_wchar_t is an alias for wchar_t): if (sizeof(alias_wchar_t) == sizeof(wchar_t)) // warning occurs here { // do stuff } else { …
Christopher Berman
  • 719
  • 1
  • 5
  • 21
17
votes
2 answers

is `warning C4127` (conditional expression is constant) ever helpful?

While answering this post, I suggested using do {...} while(0) for multiline macros. On MSVC, I found this code throws up: warning C4127: conditional expression is constant To make code warning-free, I need to choose one of these ugly…
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
9
votes
2 answers

Why MSVC generates warning C4127 when constant is used in "while" - C

For code, while(1) { /* ..... */ } MSVC generates the following warning. warning C4127: conditional expression is constant MSDN page for the warning suggests to use for(;;) instead of while(1). I am wondering what advantage for(;;) is giving…
Navaneeth K N
  • 15,295
  • 38
  • 126
  • 184
9
votes
2 answers

How to disable C4127 for do {} while(false)

Possible Duplicate: C/C++: How to use the do-while(0); construct without compiler warnings like C4127? //file error.h #define FAIL(message) \ do { \ std::ostringstream ossMsg; \ ossMsg << message; \ …
q0987
  • 34,938
  • 69
  • 242
  • 387
2
votes
2 answers

CppUnit expect exception with Assert Throw compiles with warning C4127

Currently I am writing unit tests in C++ with CppUnit. Recently I needed to check that an exception is thrown in a specific case using CppUnits macro: CPPUNIT_ASSERT_THROW( boost::get(m_boostVariantFooOrBar), …
Hannes M
  • 741
  • 7
  • 20
1
vote
2 answers

warning C4127: conditional expression is constant in cl command

#define Val_MAX 0 int main() { if(Val_MAX) printf("The value is %d",VALUE_MAX); return 0; } When I try to compile the above program if(VALUE_MAX) is showing a warning conditional expression is constant. How to solve the above…
0
votes
4 answers

odd "warning C4127: conditional expression is constant" under VS2005

I'm trying to compile LightZPng with warnings on level 4. I get a lot of C4127 on lines that are clearly not worthy of this warning. An example: #define MAX_BITS 15 int values_per_bitlen[ MAX_BITS + 1 ]; for ( int i = 0; i <= MAX_BITS; ++i ) //…
Jim Buck
  • 20,482
  • 11
  • 57
  • 74