Standard Input and OutputĀ 

C Programming TutorialĀ 

Go to the Table of Contents

Vist the Gifcom Corporation

Standard Output and Standard Input

Talking to the user.

Getting information in and out of a computer is the most important thing that a program can do. Without input and output computers would be quite useless.

C treats all its output as though it were reading or writing to different files. A file is really just an abtraction: a place where information comes from or can be sent to. Some files can only be read, some can only be written to, others can be both read from and written to. In other situations files are called I/O streams.

C has three files (also called streams) which are always open and ready for use. They are called stdin, stdout and stderr, meaning standard input and standard output and standard error file. Stdin is the input which usually arrives from the keyboard of a computer. stdout is usually the screen. stderr is the route by which all error messages pass: usually the screen. This is only `usually' because the situation can be altered. In fact what happens is that these files are just handed over to the local operating system to deal with and it chooses what to do with them. Usually this means the keyboard and the screen, but it can also be redirected to a printer or to a disk file or to a modem etc.. depending upon how the user ran the program.

The keyboard and screen are referred to as the standard input/output files because this is what most people use, most of the time. Also the programmer never has to open or close these, because C does it automatically. The C library functions covered by stdio.h provides some methods for working with stdin and stdout. They are simplified versions of the functions that can be used on any kind of file, See Files and Devices. In order of importance, they are:

 printf ()
 scanf ()
 gets ()
 puts ()