I came across a piece of code where <= is used in an algebraic expression in C.
int x=2, y=4, z=5, m=10;
m+= x * y + z++ <=m+3;
printf("%d, %d", m,z);
I've never seen the use of a relational operator in this manner and wondered how the output of this is being computed. The output that I receive when running this is 11, 6. In what way does <= work here?
This statement
may be rewritten like
due to precedence of operators.
Taking into account these declarations
the operand
(x * y + z++ )of the relational operator<=is equal to2 * 4 + 5that is equal to13(pay attention to that the value of the postfix increment operator is the value of its operand before incrementing) ,m + 3is also equal to13. So the relational operator yields1.And in fact you have
From the C Standard (6.5.8 Relational operators)
As a result
mwill be equal to11andzdue to the postfix increment operator after this statement will be equal to6.