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

北战南征 提交于 2019-12-04 03:12:25

问题


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

#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 putchar and printf?

Which is better to use between the 2?


回答1:


printf is 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.

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




回答2:


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.




回答3:


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

getchar and putchar get and put characters

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

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




回答4:


  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.



回答5:


The difference is that putchar prints one character whereas printf can print a lot more.

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 printf as then you can write

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

instead of

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



回答6:


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"!

#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



来源:https://stackoverflow.com/questions/23749797/putchar-vs-printf-is-there-a-difference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!