Why using multiple %d is not making any changes in the output | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why using multiple %d is not making any changes in the output

In this printf function Only the first %d works but the others are not doing anything(not stopping the program or not showing any garbage values in visual studio code's terminal) https://code.sololearn.com/c2hnk2FUmDmF/?ref=app https://code.sololearn.com/c2hnk2FUmDmF/?ref=app

5th Nov 2022, 2:15 PM
Riyad Rahman [π©]
Riyad Rahman [π©] - avatar
1 Answer
+ 2
Riyad Rahman [π©] printf uses only the first argument as the format string, which tells how to format the remaining arguments. If the format string does not contain enough specifiers for the remaining arguments, then it will not print the extra arguments. printf("%d",printf("%s","HELLO WOLRD!"),"%d","%d","%s"); Output: HELLO WOLRD!12 The "HELLO WOLRD!" gets printed first from the inner printf. Then the 12 gets printed from the outer printf. 12 is the return value from the inner printf (the number of characters that it printed). The remaining "%d" and "%s" arguments are treated as ordinary strings and have no specifiers in the first format string to indicate how you want them to be printed. Hence they don't get printed and you get a warning. This would show the added arguments: printf("%d %s %s %s",printf("%s","HELLO WOLRD!"),"%d","%d","%s");
5th Nov 2022, 4:23 PM
Brian
Brian - avatar