#include <stdio.h>
int main(void)
{
int a = 0, b = 1;
a = (a = 5) && (b = 0);
printf("%d", a);
printf("%d", b);
}
The value of variable a is getting updated twice, which violates the C standards. So I think it will be compiler dependent. My I ran this code in lot of online C compilers and all are giving same output.
The behavior of this code is well defined due to the sequence point introduced by the
&&operator. This is spelled out in section 6.5.13p4 of the C standard regarding the logical AND operator&&:Going through the code:
The left side of the
&&operator, i.e.(a = 5)is evaluated first, which setsato 5. This evaluates to true, so there is then a sequence point before the right side, i.e.(b = 0)is evaluated. This setsbto 0 and evaluates to false, so the&&operator results in the value 0. This value is then assigned toa.