[Solved] How the given C code be interpretated by different Compiler [closed]


The expression ++i || ++j && ++k will group like so due to precedence:

++i || (++j && ++k)

One of the few rules C has about evaluation order of expressions is that the left-hand side of the || and the `&& operators must execute before the right-hand side (for short circuiting). This is a non-optional language requirement.

That means that ++i has to be evaluated first. It will return 0 (it does not evaluate to 1 as you mistakenly mention in the question). Since that’s not enough to short circuit the || operation, the right side will need to be evaluated. A similar process will occur for evaluation the && operator (and both sides of the && operation will need to be evaluated).

The resulting output will be:

0 3 1 1 

regardless of the compiler. There is no undefined, unspecified, or implementation defined behavior evaluating that expression.

The expression ++i || ++j && ++k will group like so due to precedence:

++i || (++j && ++k)

One of the few rules C has about evaluation order of expressions is that the left-hand side of the || and the `&& operators must execute before the right-hand side (for short circuiting). This is a non-optional language requirement.

That means that ++i has to be evaluated first. It will return 0. Since that’s not enough to short circuit the || operation, the right side will need to be evaluated. A similar process will occur for evaluation the && operator (and both sides of the && operation will need to be evaluated).

The resulting output will be:

0 3 1 1 

regardless of the compiler. There is no undefined, unspecified, or implementation defined behavior evaluating that expression.


One way to get the output you mention in your question is to use the following expression instead:

m = --i || ++j && ++k;

In this case, --i evaluates to -2 and the short circuiting rule requires that the right side of the || operator not be evaluated. So j and k are left alone.

solved How the given C code be interpretated by different Compiler [closed]