0

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!

staticvoid17
  • 90
  • 2
  • 8
  • The beginners assumption that a "character" is `char` (likewise that a "number" is an `int`). Almost every C library function that involves taking a character as an argument, or returning a character uses `int`. In the case of `getchar()` we need to distinguish the data `0xFF` from the flag `EOF` (-1). That flag isn't data but a signal that there is no more input (End Of File). Also, if you look at `sizeof 'a'` you'll find it isn't `1` but the same as `sizeof (int)`. – Weather Vane Oct 30 '21 at 06:35
  • Ah right, that variable is declared as an `int` so that it can be checked whether it's equal to `EOF` or not, and `EOF` is -1 – staticvoid17 Oct 30 '21 at 06:40
  • 1
    Yes, and the function declaration is **`int`** `getchar(void);` – Weather Vane Oct 30 '21 at 06:42
  • Oh ok, thank you! – staticvoid17 Oct 30 '21 at 06:45

2 Answers2

1
  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.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
0
  • 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

  • So, I assume `EOF` is something like `ctrl+c`? – staticvoid17 Oct 30 '21 at 06:44
  • On Linux is Ctrl-D and Windows is Ctrl-Z. But Ctrl-C is handled differently. It ends the program, not the input. – Weather Vane Oct 30 '21 at 06:45
  • 1
    It is important to realize that the `EOF` value in C has nothing to do with the character typed to indicate end-of-file at the terminal. The character typed (usually ctrl-d on Unix-like systems) is never returned. Instead, the terminal device driver notices it and causes the `read()` system call to return zero bytes read, which indicates end-of-file to the higher levels. The C library again intercepts this end-of-file indication and makes `getchar()` return `EOF` (which is usually -1). – Ture Pålsson Oct 30 '21 at 06:50
  • `char` is not limited to a seven-bit number, in most C implementations. – Eric Postpischil Oct 30 '21 at 10:52