Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

C6237

warning C6237: (<zero> && <expression>) is always zero. <expression> is never evaluated and may have side effects

This warning indicates that a constant value of zero was detected on the left side of a logical-and operation that occurs in a test context. The resulting expression always evaluates to false. Therefore, the right side of the logical-AND operation is not evaluated. This is referred to as "short-circuit evaluation."

You should examine the right side of the expression carefully to ensure that any side effects such as assignment, function call, increment, and decrement operations needed for proper functionality are not affected by the short-circuit evaluation.

The expression (0 && n) produces no side effects and is commonly used to selectively choose code paths.

The following code shows various code samples that generate this warning:

#include <stdio.h>
#define INPUT_TYPE 0

int test();

// side effect: n not incremented
void f1( int n )
{
   if(INPUT_TYPE && n++) //warning: 6237
   {
      puts("code path disabled");
   }
   else
   {
      printf_s("%d - n was not incremented",n);
   }
}

// side effect: test() not called
void f2( )
{
   if(INPUT_TYPE && test()) //warning: 6237
   {
      puts("code path disabled");
   }
   else
   {
      puts("test() was not called");
   }
}

//side effect: assignment and function call did not occur
void f3( int n )
{
   if(INPUT_TYPE && ( n=test() )) //warning: 6237
   {
      puts("code path disabled");
   }
   else
   {
      printf_s("%d -- n unchanged. test() was not called", n);
   }
}

To correct this warning, use the following code:

#include <stdio.h>
#define INPUT_TYPE 0
int test();

void f1( int n )
{
if(INPUT_TYPE)
{
  if(n++)
    {
      puts("code path disabled");
    }
}
else
  {
    puts("n was not incremented");
  }
}

void f2( )
{
   if(INPUT_TYPE)
   {
      if( test() )
      {
         puts("code path disabled");
      }
   }
   else
   {
      puts("test() was not called");
   }
}

void f3( int n )
{
   if(INPUT_TYPE)
   {
     n = test();
      if( n )
      {
         puts("code path disabled");
      }
   }
   else
   {
      puts("test() was not called");
   }
}

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.