Possible Duplicate:
Compilers and argument order of evaluation in C++
cout << order of call to functions it prints?
-
This:
int k=3; printf("%d %d %d",k++,k,++k);Gives output as
4 4 4because they are pushed into the stack as:%d%d%d 4 -- for k++ 4 --for k 4 --for ++kRight?
-
This:
int k = 3; cout << k++ << k << ++k;Is actually repeated function calls, so it’s equivalent to:
( ( (cout << k++) << k) << ++k);So, I suppose first of all
k++thenkand then++kmust always be executed in this order, right? I believe a function call is a sequence point, but the outputs vary on different implementations. Why is this so?
This is undefined because there is no sequence point between the , in the printf statement. Without a sequence point the compiler is free to order writes to the memory location
kas it wills.Now you may be wondering ‘what the hell is a seqeunce point’ and why is it relevant? Basically a sequence point is a point in the code where the memory location in question, in this case
khas been modified at most once. There is a fuller description here:https://isocpp.org/wiki/faq/misc-technical-issues#double-mod-betw-seq-ptAs you can see from the FAQ, the
,in the printf does not introduce a sequence point.In the case of
coutthis is different because there are 3 function calls tooperator >>. A function call introduces a sequence point therefore the modifications to memory locationkhave a defined order. However (and this was a point I missed but Cubbi pointed out) because C/C++ doesn’t define the order of evaluation of function arguments those arguments, even if they are functions, can be evaluated in any order the compiler defines. So in the expression:f(h(), g())Whether h() or g() is evaluated first is undefined: http://www.stroustrup.com/bs_faq2.html#undefined. So this is the reason why even in the case of cout you are getting different results from different compilers, basically because the
cout << k++ << k << ++ktranslates tocout.operator<<(k++).operator<<(k).operator(++k)which is effectively an expression like this:f(h(g(cout, k++), k), ++k)and each function argument is evaluated in an unspecified order.