Udostępnij za pośrednictwem


C6235

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

This warning indicates that a non-zero constant value, other than one, was detected on the left side of a logical-or operation that occurs in a test context. The right 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 non-zero 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

The following code generates this warning because INPUT_TYPE is 2:

#define INPUT_TYPE 2
void f(int n)
{
   if(INPUT_TYPE || n) //warning 6235 issued
   {
      puts("Always gets here");
   }
   else
   {
      puts("Never gets here");
   }
}

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

#define INPUT_TYPE 2
void f(int n)
{
   if((INPUT_TYPE & n) == 2)
   {
      puts("bitwise-AND comparison true");
   }
   else
   {
      puts("bitwise-AND comparison false");
   }
}

See Also

Reference

C Logical Operators