What will be output if you will compile and execute the following c code?
void main(){
int a=10;
printf("%d %d %d",a,a++,++a);
}
(A) 12 11 11
(B) 12 10 10
(C) 11 11 12
(D) 10 10 12
(E) Compiler error
________________________________________
Explanation:
In c printf function follows cdecl parameter passing scheme. In this scheme parameter is passed from right to left direction.
So first ++a will pass and value of variable will be a=10 then a++ will pass now value variable will be a=10 and at the end a will pass and value of a will be a=12.
the output is 12 11 12
ReplyDeletenow tell the reason
output 12 11 11
DeleteOutput 10 10 12
DeleteIts an illegal operation. look here http://stackoverflow.com/questions/1270370/printfd-d-d-n-a-a-a-output
Deleteit is compiler dependent expression.
DeleteThis comment has been removed by the author.
DeleteFor the increment/decrement operator the associativity is from right to left.
Delete++a -> 12
a++ -> 11
a -> 12
12 11 12
DeleteBasically,What happens here is, same value is modified more than once and it is not defined then answer will be compiler dependent.
Deleteint i=10;
ReplyDeleteprintf("%d %d %d",++i,i++,i);
what will be output of this plz explain
Output will be 12 10 12 ,use www.codechef.com
DeleteOutput will be 12 11 12 ,use www.codechef.com
ReplyDeleteWhat will be the Output:
ReplyDeletemain()
{
int arr[]={0,1,2,3,4};
int i,*ptr;
for(ptr=arr+4,i=0;i<=4;i++)
printf("%d",ptr[-i]);
}
plz rply
output is 12 11 12.. plz tell why
ReplyDeleteoutput is 12 11 12.. plz tell why
ReplyDeletevoid main()
ReplyDelete{
int a=10;
printf("%d",a);
printf("%d",a++);
printf("%d",++a);
getch();
}
this program will get u following o/p
101012
D: 10 10 12
ReplyDeleteThe output is 12 12 10
ReplyDeleteHello everyone,
ReplyDeleteAccording to the advanced compilers, Evaluation of printf() statement takes places from right to left
++a = 11
a++ = 11// after completing value is 12
a = 12
This is correct my dears.
If You have any more doubts in regarding this question or else on anything else in any language
Please feel free to mail me.
kanumururajeshreddy@gmail.com
What will be the output of this question
ReplyDeleteVoid main()
{
int a= 120;
printf("%d"+1,a);
}
12 12 10
ReplyDelete