I’m learning to code in C. I was going through the logical expressions but then this question came and I couldn’t understand ? Like are logical operators exceptional in case of precedence. Let’s take this one example:
i = 1; j = 1; k = 1;
printf("%d ", ++i || ++j && ++k);
printf("%d %d %d", i, j, k);
The compiler answer is 1 ,2,1,1 which means it short circuited. But i can’t understand that why didn’t the compiler follow the precedence (++ has higher precedence than ||) or is it just the way it’s designed in case of logical expressions? I just want to confirm. Thanks.it shows operator precedence
There are a number of sequence points, which are markers for which prior side-effects (such as assignment/increment) must be complete by, and two of those sequence points are
&&and||. The ternarycontrol?true:falseoperator also has sequence points in it, hence why the side-effects of the first operand are complete before the second or third operands are evaluated.