#include<stdio.h>
char *fun()
{
return ("samsung India");
}
int main()
{
printf("%s",printf("electronics")+fun());
return 0;
}
Explanation:
Output will be electronicsia. When the outer printf statement will be
executed then its arguments (printf("electronics")+fun()) must be evaluated first. Wheather the inner print statement (printf("electronics")) will be called first or the fun() function will be called first that is an Undefined behaviour according to C-99, but that wont affect our output. whenever the inner printf statement will be executed it will print electrinoics and it will return 11 as printf returns the number of character printed. Now when the fun() function will be called it will return the base address of the string samsung India say 0xbfb6fc8c. Ultimately the argument of the outer printf statement will be reduced to 11+0xbfb6fc8c and it is 0xbfb6fc97 which is address of second i in India.
When the outer printf executes it prints ia. So inner printf statement prints electronics and outer printf statemtent prints ia. This can be easy to understand in this step by step method (considering fun() function is being called after the inner printf function):
printf("%s",printf("electronics")+fun());
printf("%s",11+fun()); // prints "electronics"
printf("%s",11 + 0xbfb6fc8c);
printf("%s",0xbfb6fc97); // prints "ia"
char *fun()
{
return ("samsung India");
}
int main()
{
printf("%s",printf("electronics")+fun());
return 0;
}
Explanation:
Output will be electronicsia. When the outer printf statement will be
executed then its arguments (printf("electronics")+fun()) must be evaluated first. Wheather the inner print statement (printf("electronics")) will be called first or the fun() function will be called first that is an Undefined behaviour according to C-99, but that wont affect our output. whenever the inner printf statement will be executed it will print electrinoics and it will return 11 as printf returns the number of character printed. Now when the fun() function will be called it will return the base address of the string samsung India say 0xbfb6fc8c. Ultimately the argument of the outer printf statement will be reduced to 11+0xbfb6fc8c and it is 0xbfb6fc97 which is address of second i in India.
+----+----+----+----+----+----+----+----+----+----+----+----+----+ | s | a | m | s | u | n | g | | I | n | d | i |a | +----+----+----+----+----+----+----+----+----+----+----+----+----+ ^ ^ | | 0xbfb6fc8c 11+0xbfb6fc8c
When the outer printf executes it prints ia. So inner printf statement prints electronics and outer printf statemtent prints ia. This can be easy to understand in this step by step method (considering fun() function is being called after the inner printf function):
printf("%s",printf("electronics")+fun());
printf("%s",11+fun()); // prints "electronics"
printf("%s",11 + 0xbfb6fc8c);
printf("%s",0xbfb6fc97); // prints "ia"
No comments:
Post a Comment