Monday, May 24, 2010

Explain the output of the following C code...?

int main()


{


int i=43;


printf( "%d\n", printf( "%d", printf( "%d", i)));


return 0;


}





The output is : 4321





Explain why....

Explain the output of the following C code...?
Because the number of characters are being printed as a return from printf following the value 43.





The innermost printf prints the value 43. The next outer one prints 2 because two characters were printed. The next one prints 1 because the 2 was printed.





printf( "%d\n", %26lt;- prints "1" because of the following.


printf( "%d", %26lt;- prints "2" because of the following


printf( "%d", i))); %26lt;- prints the value 43





The 43 gets printed first because it is evaluated prior to the earlier printfs
Reply:C evaluates internal expressions first, so the first print that will occur is the internal printf( "%d", i ), which will give you your:





43





The second internal printf( "%d", printf( "%d", i ) ) is then evaluated with the return from the first printf. The printf function returns the number of characters printed, in this case 2 (since it printed 43), so now you have:





432





Finally, the last printf is evaluated with the return from the second printf, which printed one character (2), so that gives you your 1 and you have:





4321

song downloads

No comments:

Post a Comment