edited by
1,134 views
0 votes
0 votes

What is the output generated by this code?

main(){
	int i=0;
	printf("%d %d %d %d %d %d %d", i++, ++i, i, i++, i, ++i, i++);
}
  1. 4 4 3 2 2 2 0
  2. 4 5 5 2 5 5 0
  3. 0 2 2 2 2 4 4
  4. Compiler Dependent
edited by

1 Answer

1 votes
1 votes

I think it is compiler dependent i.e, order in which expressions in the function are evaluated.

whether 'left to right' or 'right to left' .

1. If left to right then 0,2,2,2,3,4,4 .

printf("%d %d %d %d %d %d %d", i++, ++i, i, i++, i, ++i, i++);

                  -------------------->> --------------------------->>

               (results printing order)         (evaluation order)

2. If right to left

printf("%d %d %d %d %d %d %d", i++, ++i, i, i++, i, ++i, i++);

                   ---------------------->> <<-------------------------------

               (results printing order)         (evaluation order)

then 4,4,3,2,2,2,0

Note : order of printing the expression is same but the order of evaluation is different.

Related questions

0 votes
0 votes
0 answers
1
Ajitesh Mandal asked Apr 3, 2017
391 views
What is the output of the program?? I think the output should be 2 2 1 . But on running the program gives the output 2 3 3 . Pls explain why that output 2 3 3 in details ...
0 votes
0 votes
2 answers
3
bahirNaik asked Jan 13, 2016
674 views
After executing the program, I am getting 10 as the answer.Please explain and give the postfix for the expression [ z=x++-y*b/a ].
1 votes
1 votes
1 answer
4
admin asked Oct 5, 2015
735 views
Find the output of the below program in case of Dynamic Scoping with call by need evaluation methodint x=10,y=10;main(){int x=2;int y=3;fun1(x+y,5);printf("x");}fun1(int ...