explain the output

input

include <stdio.h>

int main(){
printf ( “%d %d”, printf ( “nice” ), printf ( “day” );
return 0;
}

output

daynice4 3

please explain the output

actually printf returns the number of char it printed…
so 4 3 corresponding to each word…
also look at dis

1 Like

This is your program

#include <stdio.h>

int main()
{
    printf("%d %d", printf("nice"), printf("day"));
    return 0;
}

There are a few things to look upon in this example:

  1. The return value of a printf() function
  2. The order of evaluation of actual parameters in a function call

The value returned by a printf statement is the number of characters it “printed”. So, printf("nice") would return 4 and printf("day") would return 3. That explains the “4 3” part in your output.

For the second part, there are different answers. But everything just boils down to the same thing – “it is compiler dependent”. Assuming a gcc compiler in a linux machine, its working can be understood as follows:

All parameters are evaluated from right-to-left.

That means, in your example, printf("day") would be the first one to be evaluated. This prints out the string “day” and then returns a value of 3. This will be the value passed as the third argument to the outermost printf (The first one is "%d %d"). Then, printf("nice") is evaluated. So, “nice” gets printed, and the value returned (4) is passed as the second argument to the outermost printf. Now, the outermost printf looks like printf("%d %d", 4, 3);

Thus, the final output is “daynice4 3

1 Like