Why transfer printf result to another printf job?

How does the following code work?

void main() { printf("%d", printf("earth")); } 

This gives as output: earth5 .

+4
source share
3 answers

The return value of printf is the number of characters printed. The internal printf is called first. Equivalent to:

 int rc = printf("earth"); printf("%d", rc); 
+6
source

This is absolutely normal :-)

A fingerprint ("ground") displays the ground and returns 5 (the number of characters printed).

Another printf gets 5 as a parameter and prints it as an integer (due to% d)

+4
source

%d expects an integer to be printed. printf returns the number of characters printed, and you print the string 5 char.

He first evaluates the internal print to find out how many characters were printed, and then evaluates the external print 5.

+2
source

Source: https://habr.com/ru/post/1436653/


All Articles