2

What will be printed as the result of the operation below:

x=5; 
printf("%d,%d,%d\n",x,x<<2,x>>2); 

Answer: 5,20,1

I thought order is undefined yet I found above as interview question on many sites.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
sunny
  • 39
  • 2
  • thanks for help with indentation . And I meant some operator like postfix or prefix instead of shift. – sunny Jul 17 '11 at 18:19
  • 1
    Why exactly do you think it'd be undefined? and why do you think order of evaluation matters? Each parameter will have the same value regardless of when it is evaluated. – abelenky Jul 17 '11 at 18:27

5 Answers5

3

Bit shift operators don't modify the value of the variable... so order doesn't matter.

Patrick87
  • 27,682
  • 3
  • 38
  • 73
3

The order of evaluation is unspecified, but it doesn't matter because you're not modifying x at all.

So the program is well-defined, and the answer is as given.

The following would have undefined semantics:

printf("%d,%d,%d\n", x, x <<= 2, x >>= 2); 
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • ALthough I don't see a downvote on your answer I can suppose it was there and the only thing I can think of is that as we discussed the behavior is not unspecified, but undefined, which makes your answer not-entirely correct... But I wouldn't downvote just for that – Armen Tsirunyan Jul 17 '11 at 18:25
3

From the C++ standard:

The order of evaluation of arguments is unspecified. All side effects of argument expression evaluations take effect before the function is entered. The order of evaluation of the postfix expression and the argument expression list is unspecified.

However, your example would only have undefined behavior if the arguments were x>>=2 and x<<=2, such that x were being modified.

jtbandes
  • 115,675
  • 35
  • 233
  • 266
1

I found the answer in c++ standards.

Paragraph 5.2.2.8:

The order of evaluation of arguments is unspecified. All side effects of argument expression evaluations take effect before the function is entered. The order of evaluation of the postfix expression and the argument expression list is unspecified.

In other words, It depends on compiler only.

joce
  • 9,624
  • 19
  • 56
  • 74
Igor
  • 4,235
  • 3
  • 34
  • 32
-1

The order of evaluation is undefined in the Official C Specification.

However, as a matter of practicality, parameters are usually evaluated right-to-left.

In your problem, the bit-shift operator doesn't change the value of X, so the order of evaluation is not important. You'd get 5,20,1, whether evaluated left-to-right, right-to-left, or middle-first.

In C, parameters are pushed on to the stack in a right-to-left order, so that the 1st param (in this case, the char* "%d,%d,%d") is at the top of the stack. Parameters are usually (but not always) evaluated in the same order they are pushed.

A problem that better illustrates what you're talking about is:

int i=1;
printf("%d, %d, %d", i++, i++, i++);

The official answer is "undefined".
The practical answer, (in the several compilers/platforms I've tried), is "3, 2, 1".

abelenky
  • 63,815
  • 23
  • 109
  • 159
  • 2
    Then you have very limited experience. Also once the compiler optimizations get a hold of if then there is no telling what will happen. – Martin York Jul 17 '11 at 18:10
  • All this pretending that you know what the compiler is doing is _dangerous_. (a) Don't do it; (b) **please** for the love of god don't teach it. – Lightness Races in Orbit Jul 17 '11 at 18:18
  • 2
    Strange, in all the implementations I've tried, your example gives "3, 3, 3". Granted, I only tested three implementations (msvc10, MingW(gcc 4.5.4) and [Ideone(gcc 4.3.4)](http://ideone.com/LNR8s)), but they are quite widely used implementations. – Benjamin Lindley Jul 17 '11 at 18:33
  • 2
    @Benjamin Lindley: I would also bet that you could get the values to change by using different optimizations. – Martin York Jul 17 '11 at 18:54