18

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?

  • 5
    `printf("%c, c);` and `putchar(c);` have identical behaviour in this example. – M.M May 20 '14 at 02:35
  • 2
    `printf("%c, c)` and `putchar(c)` function the same other than the return value differs - which is not used in this example. `putchar(c)` will certainly perform faster than `printf("%c, c)`. The degree of speed difference is highly dependent on many other factors. – chux - Reinstate Monica May 20 '14 at 18:25
  • 1
    @chux Why Would putchar be fast , and why putchar_unlocked is more faster? – Suraj Jain Mar 04 '17 at 09:30
  • 1
    @SurajJain An optimizing compiler may emit the same code for `printf("%c, c)` and `putchar(c)` and so no performance difference in that case. With a less intelligent compiler, `putchar(c)`, with its simple functionality would certainly be faster than `printf("%c, c)`, although, without testing, the degree of speed difference is unknown and may be marginal. `putchar_unlocked()` is not a standard C library function - I am unfamiliar with its details. – chux - Reinstate Monica Mar 04 '17 at 16:18

5 Answers5

33

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.

kirbyfan64sos
  • 10,377
  • 6
  • 54
  • 75
  • 5
    Also `putchar()` is shorter. In can help if you are golfing. – aloisdg Jul 16 '16 at 21:23
  • 5
    "putchar puts a character to the screen" is at best sloppy and at worst a severe misunderstanding. The buffered output functions know nothing of "screens"; they are completely oblivious of any specific hardware. The `putc` macro contains code which inserts a character in a stream. That abstraction is the beauty of the *nix (inspired) operating systems and runtime libraries and the reason why you can pipe the output of one program to the input of another, or run servers without any physical terminals. – Peter - Reinstate Monica May 24 '19 at 13:16
10

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.

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
user5181136
  • 101
  • 1
  • 2
2

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');
Jakob
  • 383
  • 5
  • 15
Tommy Ivarsson
  • 605
  • 4
  • 7
  • 1
    Did not downvote, but that is a bad example. The `putchar` calls seem better, and not even overly verbose here. Easier to read, too. – Thilo May 20 '14 at 02:09
1
  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.
0

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

ajbee
  • 3,511
  • 3
  • 29
  • 56