3,583 views
–1 votes
–1 votes
int main(){

int a =5;
printf("%d %d %d %d %d ",a++,a--,++a,--a,a);

}

Output is "4 5 5 5 5". How????

2 Answers

Best answer
1 votes
1 votes
It is compiler dependent in C.Order of evaluation of arguments of a function is not guaranteed  in C and C++.The order of evaluation of function arguments is unspecified because allowing the compiler to re-order the evaluation of the operands adds more room for optimization.

Bjarne Stroustrup also says it explicitly in "The C++ Programming Language" 3rd edition section 6.2.2, with some reasoning:-

"Better code can be generated in the absence of restrictions on expression evaluation order.".
selected by

Related questions

0 votes
0 votes
2 answers
1
logan1x asked May 19, 2019
651 views
A default catch block catches,[A]. all thrown objects[B]. no thrown objects[C]. any thrown object that has not been caught by an earlier catch block[D]. all thrown object...
2 votes
2 votes
1 answer
3
Prayag asked Jul 16, 2018
442 views
#include <stdio.h void f(char ); int main() { char *argv[] = { "ab", "cd", "ef", "gh", "ij", "kl" }; f(argv); return 0; } void f(char p) { char *t; t...
2 votes
2 votes
1 answer
4
Sugumaran asked Feb 7, 2018
483 views
#include<stdio.h int main() { int x=191; char *p; p=(char*)&x; printf("%d",*p); }explain it