C语言 我应该使用 printf("\n") 还是 putchar('\n') 在 C 中打印换行符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34231427/
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 12:15:48  来源:igfitidea点击:

Should I use printf("\n") or putchar('\n') to print a newline in C?

c

提问by Courtney Pattison

When I'm writing a program in C, I often have to print a newline by itself. I know you can do this in at least two ways: printf("\n")and putchar('\n'), but I'm not sure which way is the best choice in terms of style and possibly efficiency. Are there any best practices for using one over the other? Does it really matter?

当我用 C 编写程序时,我经常不得不自己打印一个换行符。我知道您至少可以通过两种方式做到这一点:printf("\n")putchar('\n'),但我不确定哪种方式在风格和效率方面是最佳选择。是否有使用一种的最佳做法?真的有关系吗?

回答by DaoWen

It will make no difference which one you chose if you're using a modern compiler[1]. Take for example the following C code.

如果您使用的是现代编译器[1],那么您选择哪一个都没有区别。以下面的 C 代码为例。

#include <stdlib.h>
#include <stdio.h>

void foo(void) {
    putchar('\n');
}

void bar(void) {
    printf("\n");
}

When compiled with gcc -O1(optimizations enabled), we get the following (identical) machine code in both fooand bar:

当使用gcc -O1(启用优化)编译时,我们在foo和 中得到以下(相同的)机器代码bar

movl    , %edi
popq    %rbp
jmp _putchar                ## TAILCALL

Both fooand barend up calling putchar('\n'). In other words, modern C compilers are smart enough to optimize printfcalls very efficiently. Just use whichever one you think is more clear and readable.

双方foobar最终调用putchar('\n')。换句话说,现代 C 编译器足够智能,可以printf非常有效地优化调用。只需使用您认为更清晰易读的任何一种。



  1. I do not consider MS's clto be a modern compiler.
  1. 我不认为 MScl是现代编译器。

回答by chux - Reinstate Monica

Are there any best practices for using one over the other?

是否有使用一种的最佳做法?

Let style drives the decision.

让风格来决定。

Since efficiency of execution is the same or nearly identical, use the style that best conveys the larger code's function.

由于执行效率相同或几乎相同,因此请使用最能传达较大代码功能的样式。

If the function had lots of printf(), stay with printf("\n").

如果函数有很多printf(),就留下来printf("\n")

Likewise for putchar('\n')and puts("")

同样对于putchar('\n')puts("")

回答by ikegami

printfand putcharare both stdio functions, so they both write to the same FILEhandle (rather than directly to the file descriptor).

printfputchar都是 stdio 函数,因此它们都写入相同的FILE句柄(而不是直接写入文件描述符)。

However, printfis far heavier since the first argument is a format string that needs to be scanned for replacement expressions and escapes.

然而,printf因为第一个参数是一个格式字符串,需要扫描替换表达式和转义符,所以要重得多。

So, both printf("\n")and putchar('\n')will do the same thing, but the latter will be faster.

所以,无论是printf("\n")putchar('\n')会做同样的事情,但后者会更快。

回答by David Haim

It doesn't really matter. I never encountered a case were printing to the console ever matter to someone in terms functions choice or effiency.

这并不重要。我从未遇到过打印到控制台对功能选择或效率而言对某人来说很重要的情况。

回答by nicomp

printf is much slower because the format string is parsed at runtime. Of course, the average homework program or simple Project Euler solution is so small that wasting a few CPU cycles doesn't matter anyway.

printf 慢得多,因为格式字符串是在运行时解析的。当然,一般的家庭作业程序或简单的 Project Euler 解决方案是如此之小,以至于浪费几个 CPU 周期都无关紧要。

回答by Ed Heal

I would go with putcharas the string in printfneeds to be parsed. Should be slightly quicker - but probably not a lot in it.

我会去,putchar因为printf需要解析字符串。应该稍微快一点 - 但可能不是很多。