Input and output operations can be performed in some languages, including C++, using standard streams.


Standard streams are provided by the C Standard Input and Output Library (cstdio, incorporated into a C++ program using the #include <stdio.h> directive). This library uses what are called streams to operate with physical devices such as terminals, keyboards, files or printers. Streams provide an abstraction which allows a program to interact with these devices in an uniform way; all streams have similar properties independent of the individual characteristics of the physical devices they are associated with.

Streams are handled in the cstdio library as pointers to FILE objects. A pointer to a FILE object uniquely identifies a stream, and is used as a parameter in operations involving that stream.

Three standard streams exist: stdin, stdout and stderr, which are automatically created and opened for all programs using the library. The standard streams are channels used by a computer program to communicate with its environment (typically a text terminal) that are connected before the program is executed. These streams provide standard input/output connections in Unix-like operating systems and C++ runtime environments. The three connections are called standard input, standard output and standard error.

stdio.h, which stands for "standard input/output", is the header in the C standard library for the C programming language which contains macro definitions, constants, and declarations of functions and types used for various standard input and output operations.

Functions declared in stdio.h are extremely popular, since as a part of the C standard library, they are guaranteed to work on any platform that supports C.

Background

Before standard streams were available, programs had to explicitly connect to the appropriate input and output devices, which could be an involved process requiring intricate knowledge of system-specific details.

Unix provided abstract devices and eliminated much of the complexity involved in input/output operations with the concept of a data stream - a sequence of data which can be handled until end of file is reached. A program can also read or write data without needing to declare beforehand how much data there will be or how it will be formatted. Furthermore, Unix automatically associated input and output by default – it was not necessary for the program to establish input and output for typical data processing.

Since standard streams were provided by Unix, its C runtime environment had to support standard streams as well. As a result, most C and C++ runtime environments provide equivalent functionality regardless of operating system.

Standard Input (stdin)

Standard input is the stream where input data is acquired.

Data transfer is requested using the read operation. Unless redirected, input is expected from the text terminal that initiated the program.

Standard Output (stdout)

Standard output is the stream where output data is sent.

Data transfer is requested using the write operation. Unless redirected, output is sent to the text terminal that initiated the program.

Standard Error (stderr)

Standard error is the output stream used to output error messages. It is independent of standard output and can be redirected separately.

Error messages are usually sent to the text terminal that initiated the program. It is not uncommon for standard output and standard error to be directed to the same destination.


examples GCC C++
Borland C++ Compiler
Java
home Home Page