I'm trying to understand getchar() != EOF
Asked Answered
M

9

43

I'm reading The C Programming Language and have understood everything so far. However when I came across the getchar() and putchar(), I failed to understand what is their use, and more specifically, what the following code does.

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

I understand the main() function, the declaration of the integer c and the while loop. Yet I'm confused about the condition inside of the while loop. What is the input in this C code, and what is the output.

Montevideo answered 23/5, 2012 at 13:21 Comment(1)
It just echoes back what you type into the terminal. Until you hit the end-of-input control code. Ctrl+Z on Windows, Ctrl+D on *nix.Book
T
40

This code can be written more clearly as:

main()
{
    int c;
    while (1) {
        c = getchar();            // Get one character from the input
        if (c == EOF) { break; }  // Exit the loop if we receive EOF ("end of file")
        putchar(c);               // Put the character to the output
    }
}

The EOF character is received when there is no more input. The name makes more sense in the case where the input is being read from a real file, rather than user input (which is a special case of a file).


[As an aside, generally the main function should be written as int main(void).]
Temperate answered 23/5, 2012 at 13:23 Comment(2)
@TheGameiswar please read my new addition too.Wehrmacht
EOF is not a character.Essy
D
30

getchar() is a function that reads a character from standard input. EOF is a special character used in C to state that the END OF FILE has been reached.

Usually you will get an EOF character returning from getchar() when your standard input is other than console (i.e., a file).

If you run your program in unix like this:

$ cat somefile | ./your_program

Then your getchar() will return every single character in somefile and EOF as soon as somefile ends.

If you run your program like this:

$ ./your_program

And send a EOF through the console (by hitting CTRL+D in Unix or CTRL+Z in Windows), then getchar() will also returns EOF and the execution will end.

Dupuy answered 23/5, 2012 at 13:24 Comment(3)
Nice example on polymorphism using getchar(). Reads from the file(input). Reads from the keyboard(input)Yautia
you can even ./your_program < input_filePepito
Thank you for pointing out how to get EOF through console!Dendrology
B
9

The code written with current C standards should be

#include <stdio.h>

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

The loop could be rewritten as

int c;
while (1) {
    c = getchar();
    if (c != EOF)
        putchar(c);
    else
        break;
}

this reads as

  • repeat forever
    • get the next character ("byte") of input from standard input and store it into c
    • if no exceptional condition occurred while reading the said character
      • then output the character stored into c into standard output
    • else
      • break the loop

Many programming languages handle exceptional conditions through raising exceptions that break the normal program flow. C does no such thing. Instead, functions that can fail have a return value and any exceptional conditions are signalled by a special return value, which you need to check from the documentation of the given function. In case of getchar, the documentation from the C11 standard says (C11 7.21.7.6p3):

  1. The getchar function returns the next character from the input stream pointed to by stdin. If the stream is at end-of-file, the end-of-file indicator for the stream is set and getchar returns EOF. If a read error occurs, the error indicator for the stream is set and getchar returns EOF.

It is stated elsewhere that EOF is an integer constant that is < 0, and any ordinary return value is >= 0 - the unsigned char zero-extended to an int.

The stream being at end-of-file means that all of the input has been consumed. For standard input it is possible to cause this from keyboard by typing Ctrl+D on Unix/Linux terminals and Ctrl+Z in Windows console windows. Another possibility would be for the program to receive the input from a file or a pipe instead of from keyboard - then end-of-file would be signalled whenever that input were fully consumed, i.e.

cat file | ./myprogram

or

./myprogram < file

As the above fragment says, there are actually two different conditions that can cause getchar to return EOF: either the end-of-file was reached, or an actual error occurred. This cannot be deduced from the return value alone. Instead you must use the functions feof and ferror. feof(stdin) would return a true value if end-of-file was reached on the standard input. ferror(stdin) would return true if an error occurred.

If an actual error occurred, the variable errno defined by <errno.h> would contain the error code; the function perror can be used to automatically display a human readable error message with a prefix. Thus we could expand the example to

#include <stdio.h>
#include <errno.h> // for the definition of errno
#include <stdlib.h> // for exit()
int main(void)
{
    int c;
    while ((c = getchar()) != EOF)
       putchar(c);

    if (feof(stdin)) {
        printf("end-of-file reached\n");
        exit(0);
    }
    else if (ferror(stdin)) {
        printf("An error occurred. errno set to %d\n", errno);
        perror("Human readable explanation");
        exit(1);
    }
    else {
        printf("This should never happen...\n");
        exit('?');
    }
}

To trigger the end-of-file, one would use Ctrl+D (here displayed as ^D) on a new line on Linux:

% ./a.out
Hello world
Hello world
^D
end-of-file reached

(notice how the input here is line-buffered, so the input is not interleaved within the line with output).

Likewise, we can get the same effect by using a pipeline.

% echo Hello world | ./a.out
Hello world
end-of-file reached

To trigger an error is a bit more tricky. In bash and zsh shells the standard input can be closed so that it doesn't come from anywhere, by appending <&- to the command line:

% ./a.out <&-
An error occurred. errno set to 9
Human readable explanation: Bad file descriptor

Bad file descriptor, or EBADF means that the standard input - file descriptor number 0 was invalid, as it was not opened at all.

Another fun way to generate an error would be to read the standard input from a directory - this causes errno to be set to EISDIR on Linux:

% ./a.out < / 
An error occurred. errno set to 21
Human readable explanation: Is a directory

Actually the return value of putchar should be checked too - it likewise returns EOF on error, or the character written:

while ((c = getchar()) != EOF) {
    if (putchar(c) == EOF) {
        perror("putchar failed");
        exit(1);
    }
}

And now we can test this by redirecting the standard output to /dev/full - however there is a gotcha - since standard output is buffered we need to write enough to cause the buffer to flush right away and not at the end of the program. We get infinite zero bytes from /dev/zero:

 % ./a.out < /dev/zero > /dev/full
 putchar failed: No space left on device

P.S. it is very important to always use a variable of type int to store the return value of getchar(). Even though it reads a character, using signed/unsigned/plain char is always wrong.

Beginner answered 8/8, 2017 at 22:39 Comment(2)
@TonyTannous have to ride the bounty :DWehrmacht
Thank you for your effort and helpHorripilation
L
6

getchar() function reads a character from the keyboard (ie, stdin)

In the condition inside the given while loop, getchar() is called before each iteration and the received value is assigned to the integer c.

Now, it must be understood that in C, the standard input (stdin) is like a file. ie, the input is buffered. Input will stay in the buffer till it is actually consumed. stdin is actually the standard input stream.

getchar() returns the the next available value in the input buffer.

The program essentially displays whatever that was read from the keyboard; including white space like \n (newline), space, etc.

ie, the input is the input that the user provides via the keyboard (stdin usually means keyboard). And the output is whatever we provide as input.

The input that we provide is read character by character & treated as characters even if we give them as numbers.

getchar() will return EOF only if the end of file is reached. The ‘file’ that we are concerned with here is the stdin itself (standard input).

Imagine a file existing where the input that we provide via keyboard is being stored. That’s stdin. This ‘file’ is like an infinite file. So no EOF.

If we provide more input than that getchar() can handle at a time (before giving it as input by pressing enter), the extra values will still be stored in the input buffer unconsumed. The getchar() will read the first character from the input, store it in c and printcwithputchar(c)`.

During the next iteration of the while loop, the extra characters given during the previous iteration which are still in stdin are taken during while ((c = getchar()) != EOF) with the c=getchar() part. Now the same process is repeated till there is nothing left in the input buffer.

This makes it look as if putchar() is returning a string instead of a single character at a time if more than one character is given as input during an iteration.

Eg: if input was
abcdefghijkl
the output would’ve been the same
abcdefghijkl

If you don’t want this behaviour, you can add fflush(stdin); right after the putchar(c);. This will cause the loop to print only the first character in the input provided during each iteration.

Eg: if input was
adgbad
only a will be printed.

The input is sent to stdin only after you press enter.

putchar() is the opposite of getchar(). It writes the output to the standard output stream (stdout, usually the monitor).

EOF is not a character present in the file. It’s something returned by the function as an error code.

You probably won’t be able to exit from the give while loop normally though. The input buffer will emptied (for displaying to the output) as soon as something comes into it via keyboard and the stdin won't give EOF.

For manually exiting the loop, EOF can be sent using keyboard by pressing ctrl+D in Linux and
ctrl+Z in Windows

eg:

while ((c = getchar()) != EOF)
{

   putchar(c);
   fflush(stdin);
}
printf("\nGot past!");

If you press the key combination to give EOF, the message Got past! will be displayed before exiting the program.

If stdin is not already empty, you will have to press this key combination twice. Once to clear this buffer and then to simuate EOF.

EDIT: The extra pair of parenthesis around c = getchar() in while ((c = getchar()) != EOF) is to make sure that the value returned by getchar() is first assigned to c before that value is compared with EOF.

If this extra parenthesis were not there, the expression would effectively have been while (c = (getchar() != EOF) ) which would've meant that c could have either of 2 values: 1 (for true) or 0 (for false) which is obviously not what is intended.

Liverish answered 8/8, 2017 at 14:7 Comment(0)
H
4

Maybe you got confused by the fact that entering -1 on the command line does not end your program? Because getchar() reads this as two chars, - and 1. In the assignment to c, the character is converted to the ASCII numeric value. This numeric value is stored in some memory location, accessed by c.

Then putchar(c) retrieves this value, looks up the ASCII table and converts back to character, which is printed.

I guess finding the value -1 decimal in the ASCII table is impossible, because the table starts at 0. So getchar() has to account for the different solutions at different platforms. maybe there is a getchar() version for each platform?

I just find it strange that this EOF is not in the regular ascii. It could have been one of the first characters, which are not printable. For instance, End-of-line is in the ASCII.

What happens if you transfer your file from windows to linux? Will the EOF file character be automatically updated?

Hemophilia answered 21/10, 2012 at 11:37 Comment(1)
This seems much more as new questions than a real answerLeavis
C
1
 getchar()

gets a character from input.

 c = getchar()

The value of this assignment is the value of the left side after the assignment, or the value of the character that's been read. Value of EOF is by default -1.

 ((c = getchar()) != EOF)

As long as the value stays something other than EOF or, in other words, as long as the condition stays true, the loop will continue to iterate. Once the value becomes EOF the value of the entire condition will be 0 and it will break the loop.

The additional parentheses around c = getchar() are for the compiler, to emphasize that we really wanted to do an assignment inside the condition, because it usually assumes you wanted to type == and warns you.

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

So the entire code actually echoes back what you input. It assigns the value of the characters to c inside the condition and then outputs it back in the body of the loop, ending only when the end of file is detected.

Chiapas answered 29/12, 2016 at 19:12 Comment(1)
The additional parentheses around c = getchar() are for the compiler, to emphasize that we do not want to assign the result of getchar() != EOF.Chishima
E
0

In a similar manner to the | pipe command above you can use redirection on your system to utilize the above code to display all the character contents of a file, till it reaches the end (EOF) represented by CTRL-Z or CTRL-D usually.

In console: ProgramName < FileName1.txt

And to create a copy of what is read from FileName1 you can: ProgramName < FileName1.txt > CopyOfInput.txt

This demonstrates your program in multiple ways to hopefully aid your understanding.

-Hope that helps.

Egwan answered 24/5, 2012 at 6:2 Comment(0)
A
0
main(){
int c;
while ((c = getchar()) != EOF)
   putchar(c);
}

Actually c=getchar() provides the character which user enters on the console and that value is checked with EOF which represents End Of File . EOF is encountered at last of file. (c = getchar()) != EOF is equivalent to c != EOF . Now i think this is much easier . If you any further query let me know.

Attrahent answered 19/6, 2016 at 14:25 Comment(0)
T
0

why has no one said yet that int getchar(void) returns the next input character each time it is called or EOF when it encounters end of file? That's the truth though and it is not out of the subject!

Toscanini answered 23/6, 2023 at 10:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.