C 中 printf() 和 puts() 有什么区别?
我知道您可以使用 printf()
和 puts()
进行打印。我还可以看到 printf() 允许您插入变量并进行格式化。
puts() 仅仅是 printf() 的原始版本吗?它应该用于所有可能的 printf()
而无需字符串插值吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
puts
比printf
更简单,但请注意前者会自动附加换行符。如果这不是您想要的,您可以将字符串fputs
到标准输出或使用printf
。puts
is simpler thanprintf
but be aware that the former automatically appends a newline. If that's not what you want, you canfputs
your string to stdout or useprintf
.(Zan Lynx 在评论中指出了这一点,但我认为它值得一个答案 - 鉴于已接受的答案没有提及它)。
puts(mystr);
和printf(mystr);
之间的本质区别在于,后者参数被解释为格式化字符串。如果字符串不包含任何控制字符 (%
),但如果您不能依赖它(如果mystr
> 是一个变量而不是文字),您不应该使用它。因此,将动态字符串作为
printf
的单个参数传递通常是危险的 - 并且在概念上错误:这同样适用于
fputs
与>fprintf
(但fputs
不添加换行符)。(This is pointed out in a comment by Zan Lynx, but I think it deserves an answer - given that the accepted answer doesn't mention it).
The essential difference between
puts(mystr);
andprintf(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 (ifmystr
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
:The same applies to
fputs
vsfprintf
(butfputs
doesn't add the newline).除了格式化之外,
puts
如果成功则返回一个非负整数,如果不成功则返回EOF
;而 printf 返回打印的字符数(不包括尾随 null)。Besides formatting,
puts
returns a nonnegative integer if successful orEOF
if unsuccessful; whileprintf
returns the number of characters printed (not including the trailing null).在简单的情况下,编译器将对
printf()
的调用转换为对puts()
的调用。例如,以下代码将被编译为我接下来展示的汇编代码。
在此示例中,我使用 GCC 版本 4.7.2 并使用
gcc -o hello hello.c
编译源代码。In simple cases, the compiler converts calls to
printf()
to calls toputs()
.For example, the following code will be compiled to the assembly code I show next.
In this example, I used GCC version 4.7.2 and compiled the source with
gcc -o hello hello.c
.根据我的经验,无论格式字符串如何,
printf()
都会比puts()
拖入更多代码。如果我不需要格式化,我就不会使用
printf
。但是,fwrite
到stdout
的工作速度比puts
快得多。注意:根据注释,'\0' 是一个整数常量。正确的表达式应该是
sizeof(char)
,如注释所示。In my experience,
printf()
hauls in more code thanputs()
regardless of the format string.If I don't need the formatting, I don't use
printf
. However,fwrite
tostdout
works a lot faster thanputs
.Note: per comments, '\0' is an integer constant. The correct expression should be
sizeof(char)
as indicated by the comments.puts() 将字符串 s 和尾随换行符写入标准输出。
函数 printf() 在格式字符串的控制下将输出写入标准输出,该格式字符串指定如何转换后续参数以输出。
我将利用这个机会请您阅读文档。
puts() writes the string s and a trailing newline to stdout.
The function printf() writes output to stdout, under the control of a format string that specifies how subsequent arguments are converted for output.
I'll use this opportunity to ask you to read the documentation.
是的,
printf
可以被认为是puts
的更强大版本。printf
提供使用%s
、%d
、等格式说明符格式化变量以进行输出的能力>%lf
等...Right,
printf
could be thought of as a more powerful version ofputs
.printf
provides the ability to format variables for output using format specifiers such as%s
,%d
,%lf
, etc...printf() 函数用于将字符串和变量打印到屏幕上,而 puts() 函数只允许您将字符串打印到屏幕上。
the printf() function is used to print both strings and variables to the screen while the puts() function only permits you to print a string only to your screen.
puts 是一个简单的选择,它在末尾添加一个新行,而 printf 则从格式化字符串写入输出。
请参阅
puts
的文档以及
printf
。我建议仅使用
printf
,因为这比切换方法更一致,即如果您正在调试,搜索所有 printf 比搜索puts
和printf 更轻松
。大多数时候您还想在打印输出中输出变量,因此puts
主要用于示例代码中。puts
is the simple choice and adds a new line in the end andprintf
writes the output from a formatted string.See the documentation for
puts
and for
printf
.I would recommend to use only
printf
as this is more consistent than switching method, i.e if you are debbugging it is less painfull to search all printfs thanputs
andprintf
. Most times you want to output a variable in your printouts as well, soputs
is mostly used in example code.比较
puts()
和printf()
时,尽管它们的内存消耗几乎相同,但puts()
比puts()
花费更多的时间代码>printf()。When comparing
puts()
andprintf()
, even though their memory consumption is almost the same,puts()
takes more time compared toprintf()
.