Quoting C11
, chapter §6.5.8, (emphasis mine)
Each of the operators <
(less than), >
(greater than), <=
(less than or equal to), and >=
(greater than or equal to) shall yield 1
if the specified relation is true and 0
if it is
false.107) The result has type int
.
Then you're performing a compound assignment, where it is required that
For the operators +=
and -=
only, either the left operand shall be an atomic, qualified, or
unqualified pointer to a complete object type, and the right shall have integer type; or the
left operand shall have atomic, qualified, or unqualified arithmetic type, and the right
shall have arithmetic type.
and your expression satisfies the constraint.
So yes, it is defined behaviour.
That said, from a syntax perspective, you're only safe because of the default operator precedence matches your expectation. There's no problem as such, but being explicit (and thus, sure) never hurts.
You can rewrite the expression as
result += ( a[i] > 0 );