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

发布于 2024-08-26 02:45:36 字数 175 浏览 7 评论 0原文

我知道您可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

嘦怹 2024-09-02 02:45:36

putsprintf 更简单,但请注意前者会自动附加换行符。如果这不是您想要的,您可以将字符串fputs到标准输出或使用printf

puts is simpler than printf but be aware that the former automatically appends a newline. If that's not what you want, you can fputs your string to stdout or use printf.

凌乱心跳 2024-09-02 02:45:36

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

puts(mystr);printf(mystr); 之间的本质区别在于,后者参数被解释为格式化字符串。如果字符串不包含任何控制字符 (%),但如果您不能依赖它(如果 mystr > 是一个变量而不是文字),您不应该使用它。

因此,将动态字符串作为 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

这同样适用于 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); 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 mystr 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:

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 fputs vs fprintf (but fputs doesn't add the newline).

時窥 2024-09-02 02:45:36

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

Besides formatting, puts returns a nonnegative integer if successful or EOF if unsuccessful; while printf returns the number of characters printed (not including the trailing null).

紫罗兰の梦幻 2024-09-02 02:45:36

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

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

#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

在此示例中,我使用 GCC 版本 4.7.2 并使用 gcc -o hello hello.c 编译源代码。

In simple cases, the compiler converts calls to printf() to calls to 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.

吾家有女初长成 2024-09-02 02:45:36

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

如果我不需要格式化,我就不会使用 printf。但是,fwritestdout 的工作速度比 puts 快得多。

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

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

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

If I don't need the formatting, I don't use printf. However, fwrite to stdout works a lot faster than puts.

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

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

倒数 2024-09-02 02:45:36
int puts(const char *s);

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

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

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

我将利用这个机会请您阅读文档。

int puts(const char *s);

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

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

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.

余生一个溪 2024-09-02 02:45:36

是的,printf 可以被认为是 puts 的更强大版本。 printf 提供使用 %s%d等格式说明符格式化变量以进行输出的能力>%lf 等...

Right, printf could be thought of as a more powerful version of puts. printf provides the ability to format variables for output using format specifiers such as %s, %d, %lf, etc...

扭转时空 2024-09-02 02:45:36

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.

ζ澈沫 2024-09-02 02:45:36

puts 是一个简单的选择,它在末尾添加一个新行,而 printf 则从格式化字符串写入输出。

请参阅 puts 的文档
以及 printf

我建议仅使用 printf,因为这比切换方法更一致,即如果您正在调试,搜索所有 printf 比搜索 putsprintf 更轻松。大多数时候您还想在打印输出中输出变量,因此 puts 主要用于示例代码中。

puts is the simple choice and adds a new line in the end and printfwrites 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 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.

半窗疏影 2024-09-02 02:45:36

比较 puts()printf() 时,尽管它们的内存消耗几乎相同,但 puts()puts() 花费更多的时间代码>printf()。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文