C语言 putchar() 与 printf() - 有区别吗?

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

putchar() vs printf() - Is there a difference?

cprintfgetcharputchar

提问by kirbyfan64sos

I am currently in chapter 1.5.1 File copying and made a program like so:

我目前在第 1.5.1 章文件复制并制作了一个程序,如下所示:

#include <stdio.h>

/* copy input to output; 1st version */
main()
{
    int c;

    c = getchar();
    while (c != EOF) {
        putchar(c);
        c = getchar();
    }
}

If I ran it like this:

如果我像这样运行它:

PS <..loc..> cc copy-0.c
PS ./a
Black
Black
White
White
Gray
Gray

The output is what I input.

输出就是我输入的。

And here's a program I made for experimental purposes:

这是我为实验目的制作的程序:

#include <stdio.h>

/* copy input to output; 1st version */
main()
{
    int c;

    c = getchar();
    while (c != EOF) {
        printf("%c",c);
        c = getchar();
    }
}

It produces the same result but is there a difference between putcharand printf?

它产生相同的结果,但putchar和之间有区别printf吗?

Which is better to use between the 2?

两者之间使用哪个更好?

回答by kirbyfan64sos

printfis a generic printing function that works with 100 different format specifiers and prints the proper result string. putchar, well, puts a character to the screen. That also means that it's probably much faster.

printf是一个通用的打印函数,可以使用 100 种不同的格式说明符并打印正确的结果字符串。putchar,好吧,将一个字符放到屏幕上。这也意味着它可能要快得多。

Back to the question: use putcharto print a single character. Again, it's probably much faster.

回到问题:用于putchar打印单个字符。同样,它可能要快得多。

回答by user5181136

I compiled an example using printf("a") with -S and got call putchar in the assembly code. Looks like when you have only one char in the printf the compiler turns it into a putchar(). I did another example using printf("ab") and got call printf, with the text section in the %edi register.

我使用 printf("a") 和 -S 编译了一个示例,并在汇编代码中调用了 putchar。看起来当 printf 中只有一个字符时,编译器会将其转换为 putchar()。我使用 printf("ab") 做了另一个例子,得到了调用 printf,文本部分位于 %edi 寄存器中。

回答by Cooooookie

That's an amazing Question, though it's been 5 years from it was asked.
Currently I'm learning something about multi-process and do some exercise with C
and I found a puzzled problem that : when using fork()
the child process can obtain what "happened before"!

这是一个了不起的问题,尽管它已经被问到 5 年了。
目前我正在学习有关多进程的知识并用 C 做一些练习,
我发现了一个令人困惑的问题:使用 fork() 时
,子进程可以获得“之前发生过的”!

#include <stdio.h>
#include <unistd.h>
int main(void) 
{      
int p1;                                                                                                                 putchar('x');
printf("this is father!\n");
printf("fork() begin!\n");
putchar('a');
p1 = fork();
while(p1==-1);
if(p1==0)
putchar('b');
else
putchar('a');
putchar('y');
putchar(10);
printf("%s\n",(!p1)? "father":"son");
}         

the output is

输出是

"xthis is father!
fork() begin!
aay
aby
son
father "

Weird? both parent and child process putchar('a') which is before fork()!
and you can even millions putchar() before the fork(), and they all been run by both parent and child process.However, printf hasn't been affected.
I try to figure it and look for it in stackOverflow and found this ask and i guess that it has something about the buff, like the putchar() and printf they both set a buff and print the string once it meets a \n so in this situation, child process copies the parent's buff so it will seemingly roll back the code and execute the previous putchar

奇怪的?父子进程 putchar('a') 在 fork() 之前!
您甚至可以在 fork() 之前使用数百万个 putchar(),并且它们都由父进程和子进程运行。但是,printf 没有受到影响。
我试着弄清楚它并在 stackOverflow 中寻找它并发现了这个问题,我猜它有一些关于 buff 的东西,比如 putchar() 和 printf 他们都设置了一个 buff 并在遇到 \n 时打印字符串所以在在这种情况下,子进程会复制父进程的 buff,因此它似乎会回滚代码并执行之前的 putchar

回答by Tommy Ivarsson

The difference is that putcharprints one character whereas printfcan print a lot more.

不同之处在于putchar打印一个字符而printf可以打印更多字符。

printf("%s\n", "this is a lot longer than one character");

Generally when you print something to the terminal you want to end it with a newline character, '\n'. At the very least for that reason I would suggest using printfas then you can write

通常,当您将某些内容打印到终端时,您希望以换行符'\n'. 至少出于这个原因,我建议使用printfas then you can write

printf("%c\n", c);

instead of

代替

putchar(c);
putchar('\n');

回答by jt25

printf lets you format strings in a complicated way, substituting things like integers and floats and other strings.

printf 允许您以复杂的方式格式化字符串,替换诸如整数和浮点数以及其他字符串之类的东西。

getchar and putchar get and put characters

getchar 和 putchar 获取和放置字符

I can say that printf is more useful in more ways compared to putchar.

我可以说 printf 在更多方面比 putchar 更有用。

Better look in their respective manual pages ( man 3 printf man 3 putchar ) in terminal

最好在终端中查看他们各自的手册页( man 3 printf man 3 putchar )

回答by jt25

  1. Putchar : prints only a single character on the screen as the syntax tells.
  2. Printf : printf line or word on the screen. Hence when you want to display only one character on the screen the use putchar. To read a string use gets function. To display string you can use puts() or printf both.
  1. Putchar :按照语法在屏幕上只打印一个字符。
  2. printf : printf 屏幕上的行或字。因此,当您只想在屏幕上显示一个字符时,请使用 putchar。要读取字符串,请使用gets 函数。要显示字符串,您可以同时使用 puts() 或 printf 。