Question

char* fun()
{
  return("samsung india");
}
main()
{
  printf("%s",printf("electronics")+fun());
}

Output: "electronicsia"

could somebody please explain me this output ?

Was it helpful?

Solution

printf("electronics") sends the output to stdout and returns the number of characters printed, which is 11, and since fun() returns a char-pointer, you are adding 11 to that pointer before the outer printf sends it to stdout.

So the inner printf outputs "electronics" and after that the outer printf outputs "ia".

OTHER TIPS

I would say that output can be different on each runtime.. because what happen there:

printf("%s",printf("electronics")+fun());

assume this: printf("%s", arg); where your arg = printf("electronics")+fun()

arg is suposse to be an char* so the pointer... actually when your arg is evaluated, printf("electronics") provide output electronics and return value 11(number of character which was successfully print), your fun() return char* - pointer to memory which points to samsung india, but you add to that pointer 11, so your pointer will be pointing to the "ia" and this add to the output...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top