Request related to printf

Here is a simple one line program using printf:

void main()

{
printf("%d%d",printf("Cis"),printf("good"));
}

Output:

goodCis34

How to explain this conclusion?

+5
source share
5 answers

The reason why the printed goodand Cislies in the fact that the parameters must be evaluated before can cause the upper level printf().

Then the return values ​​are printed.

Note that C does not determine the order in which parameters are evaluated. There are no sequence points in the instruction. Therefore, the order is undefined. And the result may appear in any order. (hence why in this case they look inactive)

+6
source

Printf . "Cis" - 3 , "" - 4.

.

, "Cis" 3, "good" 4. , undefined "Cisgood", "goodCis".

printf "34".

+3
printf("%d%d",printf("Cis"),printf("good"));

. printf ( "good" ) . "good" 4 ( ) . printf ( "Cis" ). "Cis" 3. : printf ( "% d% d", 3, 4);

, 34.

+1

, good. , Cis.

Finally, when performing the leftmost operation, it uses the corresponding lengths of both lines to populate% d notes.

0
source

printfand his family returns the number of characters printed. In your case, this is just the length of the lines. However, the order with arguments is estimated unspecified. In your case, the second external argument printfis executed first. It could be completely different.

0
source

All Articles