The two functions are equal, but the function puts()
adds a line break equal to printf("\n")
But when using the puts()
we have to be aware that only prints the string, we do not have control of the printf()
where we can print something like printf("int: %d, float: %f", x, y)
#include <stdio.h>
main() {
printf("Hello world!");
return 0;
}
In this example of Hello world compiler converts the printf()
for puts()
push rbp
mov rbp,rsp
mov edi,str.Helloworld!
call dword imp.puts
mov eax,0x0
pop rbp
ret
To conclude, the printf()
is a little slower because you need to format the variables and concatenate to a string to be printed. But this difference is practically irrelevant.
Enlightening, since I’m just starting I’ll take it as a practice, when I just want to write texts like "Hello word!" I will use the
puts()
, so the compiler has one less task, and gain a little more in performance...msm the difference is practically irrelevant...grain, grain... kkk.– J. Ferreira
@J.Ferreira in general experienced programmers do not do this, nowadays there are zero reasons to use the
puts()
.– Maniero
These things I don’t understand, if the command has its advantages or at least has no disadvantages and in the end the compiler uses it too, because the community doesn’t use it? is there anywhere where these good practices are cited? (Rhetorical questions that would accept answers)
– J. Ferreira
the
puts()
has many limitations, that’s why not used, even because the performance is equal toprintf()
that difference the compiler makes is insignificant– Fábio Morais