Beside the other well-explained answers with any side-concerns covered, I would like to give a precise and concise answer to the provided question.
Why is printf
with a single argument (without conversion specifiers) deprecated?
A printf
function call with a single argument in general is not deprecated and has also no vulnerabilities when used properly as you always shall code.
C Users amongst the whole world, from status beginner to status expert use printf
that way to give a simple text phrase as output to the console.
Furthermore, Someone have to distinguish whether this one and only argument is a string literal or a pointer to a string, which is valid but commonly not used. For the latter, of course, there can occur inconvenient outputs or any kind of Undefined Behavior, when the pointer is not set properly to point to a valid string but these things can also occur if the format specifiers are not matching the respective arguments by giving multiple arguments.
Of course, It is also not right and proper that the string, provided as one and only argument, has any format or conversion specifiers, since there is no conversion going to be happen.
That said, giving a simple string literal like "Hello World!"
as only argument without any format specifiers inside that string like you provided it in the question:
printf("Hello World!");
is not deprecated or "bad practice" at all nor has any vulnerabilities.
In fact, many C programmers begin and began to learn and use C or even programming languages in general with that HelloWorld-program and this printf
statement as first ones of its kind.
They wouldn´t be that if they were deprecated.
In a book that I'm reading, it's written that printf
with a single argument (without conversion specifiers) is deprecated.
Well, then I would take the focus on the book or the author itself. If an author is really doing such, in my opinion, incorrect assertions and even teaching that without explicitly explaining why he/she is doing so (if those assertions are really literally equivalent provided in that book), I would consider it a bad book. A good book, as opposed to that, shall explain why to avoid certain kind of programming methods or functions.
According to what I said above, using printf
with only one argument (a string literal) and without any format specifiers is not in any case deprecated or considered as "bad practice".
You should ask the author, what he meant with that or even better, mind him to clarify or correct the relative section for the next edition or imprints in general.
printf("Hello World!")
is not the same asputs("Hello World!")
.puts()
appends a'\n'
. Instead compareprintf("abc")
tofputs("abc", stdout)
– Berberidaceousputs
adds a newline? man7.org/linux/man-pages/man3/puts.3.html – Passifloraceousprintf
is also about 50% slower thanfputs
. – Forlornprintf
is deprecated in the same way that for examplegets
is deprecated in C99, so you may consider editing your question to be more precise. – Harborage