View Single Post
Newbie Member
29Jan2008,00:13  
humhaingaurav's Avatar
Hi,

One of the solution which you have posted in "Multiple choice C++ question" is wrong.

Question is:
/*question number 10*/
Code:

int a=10,b; b=a++ + ++a; printf("%d,%d,%d,%d",b,a++,a,++a);


what will be the output when following code is executed
Choice 1
12,10,11,13
Choice 2
22,10,11,13
Choice 3
22,11,11,11
Choice 4
12,11,11,11
Choice 5
22,13,13,13[Ans]

The correct answer will be 22,13,14,14
Because if you look at the operator precedence then PreIncerement operator has higher precedence than print operator. Therefore, when we write printf("%d,%d,%d,%d",b,a++,a,++a);
so here 'a++' then '++a' gets executed and then 'a' gets executed therefore we get result as 22,13,14,14.

Hope you will correct this.

Thanks
~Gaurav