-8

I have a code below and wanted to know what could be output.What i would like to see how different compiler would interpret this particular piece of code.

int main()
{
int i = -1, j = 2, k = 0, m;
m = ++i || ++j && ++k;
printf("\n %d %d %d %d \n", i, j, k, m);
return 0;
}

Now the question is while handling this code would compiler go by the rule which says

This expresion can be seen as

 m = ++i || (++j && ++k); 

as && has higher precedence over || and the result would be 0 2 0 1

or

This expresion can be seen as

m = ++i || (++j && ++k);

but compiler still will try to short circuit. so it evaluates ++i, since it's 1,(++j&&++k) >are not evaluated.so ans is 0 3 1 1

Community
  • 1
  • 1
Amit Singh Tomar
  • 8,380
  • 27
  • 120
  • 199
  • 1
    You can either read the spec and find out (better), or run the code and find out (faster). What is the purpose of asking this question here? – Jon Jul 26 '11 at 10:47
  • I wanted to know about this circuit rule.. – Amit Singh Tomar Jul 26 '11 at 10:49
  • @Jon did you read this post or just by seeing it you made it close?? – Amit Singh Tomar Jul 26 '11 at 10:50
  • You have the same (wrong) result in both cases! – MByD Jul 26 '11 at 10:51
  • Here you go: http://stackoverflow.com/questions/2108467/is-short-circuit-evaluation-guaranteed-in-c-as-it-is-in-java – Jon Jul 26 '11 at 10:51
  • 3
    Similar to http://stackoverflow.com/questions/6783105/does-the-order-of-operations-change-within-an-if-expression/6783458#6783458, answer is the same. Short circuit means that it short circuits. It doesn't mean that it doesn't short-circuit. Hence, of your two options the correct one is the one that short-circuits, not the one that doesn't. Not sure about what you claim it prints, though. Anyway `++i` is 0, so short-circuiting has no effect in this example. – Steve Jessop Jul 26 '11 at 10:52
  • I Run this code on different compiler and on different compilers am getting different Output ,why so?? – Amit Singh Tomar Jul 26 '11 at 10:59
  • "so it evaluates ++i, since it's 1,..." but you initialized `i = -1`, so `++i` should evaluate to 0... – Karl Knechtel Jul 26 '11 at 11:41
  • @Amit: either you aren't running this code, or you aren't reporting what it really output. There is no way that `i` starts at -1 and ends up -2. [Edit: oh, hang on, you forgot to include ``, so the program has undefined behavior. That could explain incorrect output, and also why it differs on different implementations.] – Steve Jessop Jul 26 '11 at 11:53
  • I wonder why are there a rash of questions about short circuiting lately? – Michael Burr Jul 26 '11 at 17:43

1 Answers1

2

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.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760