I've seen this in code where just a normal string without any formatting is displayed using printf (e.g. printf("Hello World!\n"). Why not just use puts("Hello World") instead?
-
4Because maybe next you'll want to write `printf("Hello, %s\n", name)`? – larsks Sep 02 '15 at 16:24
-
1See http://stackoverflow.com/questions/2454474/what-is-the-difference-between-printf-and-puts-in-c – michaelrccurtis Sep 02 '15 at 16:25
-
5Because people don’t know about `puts`. – Ry- Sep 02 '15 at 16:26
-
The `puts()` variant use one byte fewer memory. – alk Sep 02 '15 at 18:46
-
http://bytes.com/topic/c/answers/527094-puts-vs-printf – udit043 Sep 03 '15 at 02:18
-
Compilers optimize this for you anyway, and having the `\n` explicit instead of implicit is not a bad thing. – Peter Cordes Feb 05 '20 at 17:21
5 Answers
puts
or fputs
should be used whenever no variables must be formatted/printed. The following would give a problem:
printf("How many %s is the interest?\n");
Especially when the string to print is passed as a variable, it can be hard to find the error.

- 25,048
- 4
- 23
- 41
printf()
is generally the preferred alternative, simply because it's more powerful and gives a programmer more control over their output.

- 4,208
- 1
- 19
- 42
When you want to update your code later down the road, you might be using variables. puts()
doesn't format variables, but printf()
does. Also, puts()
always adds the new line, so if you ever don't want a newline, then you'll want to switch to printf()
. Basically, printf()
allows you to maintain the code longer.

- 1,124
- 12
- 16
-
1
-
When you're maintaining thousands of lines of legacy code and multiple files, it becomes more complicated. – ShiningLight Sep 02 '15 at 16:30
Why use
printf("mystring\n")
instead of justputs("mystring")
?
Note: there are reasons not to use printf()
like the apperance of the '%'
character, but OP seems to want reasons for printf(line_of_text)
.
Example 1: in an embedded application (code space limited) that already uses printf()
, puts()
may cost additional program space.
Example 2: The output of puts()
appends a '\n'
. This behavior catches programmers who forget this and use puts("mystring\n")
. Using printf("mystring\n")
is explicit. Using fputs("mystring\n", stdout)
is even better.

- 143,097
- 13
- 135
- 256