Udostępnij za pośrednictwem


C6236

warning C6236: (<expression> || <non-zero constant>) is always a non-zero constant

This warning indicates that a non-zero constant value, other than one, was detected on the right side of a logical-or operation that occurs in a test context. The left side of the logical-or operation is not evaluated because the resulting expression always evaluates to true. This is referred to as "short-circuit evaluation."

A constant value other than one suggests that the bitwise-AND operator (&) may have been intended. This warning is not generated for the common idiom when the non-zero constant is 1, because of its use for selectively enabling code paths, but it is generated if the non-zero constant evaluates to 1, for example 1+0.

Example

In the following code, n++ is not evaluated because INPUT_TYPE is greater than 1:

#define INPUT_TYPE 2
#include <stdio.h>

void f( int n )
{
   // side effect: n not incremented
   if( n++ || INPUT_TYPE ) //warning 6236 issued
   {
      puts( "Always gets here" );
   }
   else
   {
      puts( "Never enters here" );
   }
}

The following code uses bitwise-AND (&)operator to correct this warning:

#define INPUT_TYPE 2
#include <stdio.h>

void f( int n )
{
   if( n++ & INPUT_TYPE )
   {
      puts( "Bitwise-AND comparison is true" );
   }
   else
   {
      puts( "Bitwise-AND comparison is false" );
   }
}

See Also

Other Resources

C++ Bitwise Operators