ISO/IEC 9899:202x (E) working draft — February 5, 2020 C17..C2x N2479:

6.5.14 Logical OR operator:

  1. If the first operand compares unequal to 0, the second operand is not evaluated.

Context: There is one C compiler, which generates warning condition is always true / false for expressions involving logical operators. For example, if in expression a || b variable b is unequal to 0, then the compiler generates condition is always true (mentioning the position of b in the source code).

Question: Can the fact of generation of such a warnings be interpreted as a violation of 6.5.14.4? Please, provide explanation / argumentation / references.

Note: gcc / clang / cl (configured with the highest warning level) does not generate any warnings for the example above.

UPD. MRE:

int main(void)
{
    int c1 = 1, c2 = 1, r = 0;
    if ( c1 || c2 ) { r = 1; }
    return r;
}
$ cc x.c
x.c:4:10: warning: condition is always true
x.c:4:16: warning: condition is always true
1

There are 1 answers

0
Eric Postpischil On BEST ANSWER

While one might argue the only way the compiler could have known the value of the second operand of || was to have evaluated it, and this violates the prohibition on evaluation in C 2018 6.5.14 4 (“… If the first operand of an || compares unequal to 0, the second operand is not evaluated”) if the first operand compares unequal to 0, it is understood that “evaluation” refers to evaluations made during program execution, not translation. The C conceptual models described in C 2018 5.1 separate translation and execution. 6.5.14 4 is a prohibition on evaluation during program execution, not during translation.