UC3M

Telematic/Audiovisual Syst./Communication Syst. Engineering

Systems Architecture

September 2017 - January 2018

8.2.  I/O functions character by character

This section focuses on how to tell our program to read a character from the user (typically through the keyboard) and to write it out (typically to the screen terminal).

8.2.1.  The getc function

The getc function reads the next character from a file stream, and returns the character as an integer.

#include <stdio.h>
int getc(FILE *stream);

Here FILE *stream represents a file stream variable. If an end-of-file or error occurs, the function returns EOF.

Note

Don't worry about the FILE data structure; now we are gonna use the stdin and stdout file streams, which are predefined. On the other side, EOF is a constant defined in the header file stdio.h. Usually, its value is -1, but keep using EOF, instead of -1; that way, if you later use the program in other compile or operating system that uses a different value.