C语言 C 中的 printf() 和 puts() 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2454474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:51:53  来源:igfitidea点击:

What is the difference between printf() and puts() in C?

cstringoutputprintfputs

提问by alex

I know you can print with printf()and puts(). I can also see that printf()allows you to interpolate variables and do formatting.

我知道你可以用printf()和打印puts()。我还可以看到它printf()允许您插入变量并进行格式化。

Is puts()merely a primitive version of printf(). Should it be used for every possible printf()without string interpolation?

是否puts()仅仅是一种原始的版本printf()。是否应该在printf()没有字符串插值的情况下用于所有可能的情况?

采纳答案by Michael Kristofik

putsis simpler than printfbut be aware that the former automatically appends a newline. If that's not what you want, you can fputsyour string to stdout or use printf.

putsprintf比前者简单但要注意前者会自动附加换行符。如果这不是您想要的,您可以fputs将字符串转换为 stdout 或使用printf.

回答by leonbloy

(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).

(这是在 Zan Lynx 的评论中指出的,但我认为它值得回答 - 鉴于已接受的答案没有提到它)。

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 mystris a variable instead of a literal) you should notuse it.

puts(mystr);和之间的本质区别在于printf(mystr);,在后者中,参数被解释为格式化字符串。其结果将是往往是相同的(除了加入新行),如果字符串不包含任何控制字符(%),但如果你不能依赖于(如果mystr是一个变量,而不是文字),你应该 使用它。

So, it's generally dangerous -and conceptually wrong- to pass a dynamic string as single argument of printf:

因此,将动态字符串作为 的单个参数传递通常是危险的 - 并且在概念上是错误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 fputsvs fprintf(but fputsdoesn't add the newline).

这同样适用于fputsvs fprintf(但fputs不添加换行符)。

回答by echristopherson

Besides formatting, putsreturns a nonnegative integer if successful or EOFif unsuccessful; while printfreturns the number of characters printed (not including the trailing null).

除了格式化之外,puts如果成功或EOF不成功,则返回一个非负整数;whileprintf返回打印的字符数(不包括尾随的空值)。

回答by Hannu Balk

In simple cases, the compiler converts calls to printf()to calls to puts().

在简单的情况下,编译器将调用转换printf()为调用puts()

For example, the following code will be compiled to the assembly code I show next.

例如,下面的代码将被编译成我接下来展示的汇编代码。

#include <stdio.h>
main() {
    printf("Hello world!");
    return 0;
}
push rbp
mov rbp,rsp
mov edi,str.Helloworld!
call dword imp.puts
mov eax,0x0
pop rbp
ret

In this example, I used GCC version 4.7.2 and compiled the source with gcc -o hello hello.c.

在这个例子中,我使用了 GCC 版本 4.7.2 并使用gcc -o hello hello.c.

回答by Thomas Matthews

In my experience, printf()hauls in more code than puts()regardless of the format string.

根据我的经验,无论格式字符串如何,都会产生printf()更多的代码puts()

If I don't need the formatting, I don't use printf. However, fwriteto stdoutworks a lot faster than puts.

如果我不需要格式,我不使用printf. 然而,fwritestdout作品很多比快puts

static const char my_text[] = "Using fwrite.\n";
fwrite(my_text, 1, sizeof(my_text) - sizeof('
int puts(const char *s);
'), stdout);

Note: per comments, '\0' is an integer constant. The correct expression should be sizeof(char)as indicated by the comments.

注意:根据评论,'\0' 是一个整数常量。正确的表达方式应sizeof(char)如注释所示。

回答by Justin Ethier

Right, printfcould be thought of as a more powerful version of puts. printfprovides the ability to formatvariables for output using format specifiers such as %s, %d, %lf, etc...

是的,printf可以被认为是更强大的puts. printf提供能力格式的变量用于使用输出格式说明如%s%d%lf等...

回答by Justin Ethier

int printf(const char *format, ...);

puts()writes the string s and a trailing newline to stdout.

puts()将字符串 s 和尾随的换行符写入标准输出。

##代码##

The function printf()writes output to stdout, under the control of a format string that specifies how subsequent arguments are converted for output.

函数printf()在格式字符串的控制下将输出写入标准输出,该字符串指定如何转换后续参数以供输出。

I'll use this opportunity to ask you to read the documentation.

我将借此机会请您阅读文档。

回答by Wesley Nyandika

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.

printf() 函数用于将字符串和变量打印到屏幕上,而 puts() 函数只允许您将字符串打印到屏幕上。

回答by Johan Engblom

puts is the simple choice and adds a new line in the end and printf writes the output from a formatted string. Documentation for puts: http://linux.die.net/man/3/putsand for printf: http://linux.die.net/man/3/printf

puts 是一个简单的选择,并在最后添加一个新行, printf 写入格式化字符串的输出。puts 文档:http: //linux.die.net/man/3/puts和 printf文档:http: //linux.die.net/man/3/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 than puts and printf. Most times you want to output a variable in your printouts as well, so puts is mostly used in example code.

我建议只使用 printf,因为这比切换方法更一致,即如果您正在调试,搜索所有 printfs 比 puts 和 printf 更不痛苦。大多数情况下,您还想在打印输出中输出一个变量,因此 puts 主要用于示例代码。

回答by thil

When comparing puts()and printf(), even though their memory consumption is almost the same, puts()takes more time compared to printf().

比较puts()and 时printf(),即使它们的内存消耗几乎相同,puts()printf().