Home > Net >  Questions regarding getchar and putchar in C (K&R)
Questions regarding getchar and putchar in C (K&R)

Time:10-31

so I'm learning C by following the book, 'The C Programming Language 2nd Edition' by Dennis Ritchie and Brian Kernighan. In section 1.5.1 File Copying, the following program is shown:

#include <stdio.h>

/* copy input to output; 1st version */
main()
{
    int c;
    
    c = getchar()
    while (c != EOF) {
        putchar(c);
        c = getchar();
    }
}

and I had a few questions here.

  1. Why is the variable c declared as an int? Because it's a getchar() function shouldn't it be of type char?
  2. What is the difference between putchar and printf?
  3. I don't get what is the EOF mentioned in the condition of the while loop

Thanks!

CodePudding user response:

  1. Why is the variable c declared as an int? Because it's a getchar() function shouldn't it be of type char?

getchar cannot return just char because it needs to return either a character (if the operation succeeded) or EOF (if the operation failed).

Therefore getchar returns an int. If the operation successfully read a character, it returns the character as an unsigned char value. (This is one reason that operations with characters ought to prefer to use the unsigned char type. Avoid using char.) If the operation did not read a character, either because there are no more characters in the input stream or because an error occurred, then getchar returns EOF. EOF has some negative value, so it is always different from an unsigned char value.

If you assign the result of getchar to a char, the int value will be converted to a char. In a typical C implementation, there are 257 possible return values from getchar—256 character values and one EOF value, but a char has only 256 possible values. Therefore, the conversion will map at least two different values to the same thing, usually EOF and some other character. Then it is impossible to tell those two things apart from the char value. You need to use an int to preserve the original getchar return value.

  1. What is the difference between putchar and printf?

putchar sends a single character to the standard output stream.

printf performs formatting operations and sends the results to the standard output stream. Even in the simplest case where printf is given only a format string with no arguments to be converted, it is still given a string, not a single character.

  1. I don't get what is the EOF mentioned in the condition of the while loop

EOF stands for “End Of File,” but it is return by getchar if either the end of the stream is reached or if an error occurs.

CodePudding user response:

  • char is just a 7-bit number. The computer encodes those numbers to a character using whatever character encoding your computer uses(mostly ASCII). Most C libraries function even used int to do something related to handling characters. In this case, getchar() returns an int, so using int in this case is intentional.

  • putchar() just print one character base on the input value, while printf() can do anything you like.

  • The EOF ("end of file") character is received when there is no more input.while (c != EOF) will exit the loop if we receive EOF

  • Related