What is the difference between printf() and puts() in C?
I know you can print with printf()
and puts()
. I can also see that printf()
allows you to interpolate variables and do formatting.
Is puts()
merely a primitive version of printf()
. Should it be used for every possible printf()
without string interpolation?
Solution 1:
puts
is simpler than printf
but be aware that the former automatically appends a newline. If that's not what you want, you can fputs
your string to stdout or use printf
.
Solution 2:
(This is pointed out in a comment by Zan Lynx, but I think it deserves an aswer - given that the accepted answer doesn't mention it).
The essential difference between puts(mystr);
and printf(mystr);
is that in the latter the argument is interpreted as a formatting string. The result will be often the same (except for the added newline) if the string doesn't contain any control characters (%
) but if you cannot rely on that (if mystr
is a variable instead of a literal) you should not use it.
So, it's generally dangerous -and conceptually wrong- to pass a dynamic string as single argument of printf
:
char * myMessage;
// ... myMessage gets assigned at runtime, unpredictable content
printf(myMessage); // <--- WRONG! (what if myMessage contains a '%' char?)
puts(myMessage); // ok
printf("%s\n",myMessage); // ok, equivalent to the previous, perhaps less efficient
The same applies to fputs
vs fprintf
(but fputs
doesn't add the newline).