Edit

Share via


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 isn't evaluated because the resulting expression always evaluates to true. This language feature is referred to as "short-circuit evaluation."

Remarks

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

Code analysis name: NONZEROLOGICALOR

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 C6235 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

C Logical Operators