Standard streams in C language

February 08, 2015 , 0 Comments

A stream is nothing but a collection or sequence of bytes that is moved from main memory and I/O devices. The standard streams in ‘C’ are predefined streams. They are automatically opened whenever the program is executed. When the “main()” function of a C program is invoked, it already has predefined streams open and available for use. Two of these streams represent the standard input and output channels that will be opened for the transfer of data in the form of bytes. These streams are declared in the header file ‘stdio.h’. in total there are FIVE standard streams available, which are:

stdin : the standard input stream, which is the normal source of input for the program. The stream of data is transferred from standard input device like ‘keyboard’ to main memory referred in the form of variables.

stdout :  the standard output stream, which is used for normal output from the program. The stream of data is transferred from main memory referred in the form of variables to the output device like ‘monitor’.

stderr:  the standard error stream, which is used to output error messages and diagnostics from the program.

stdaux : the standard auxiliary device stream, which is used for the connected secondary devices.

stdprn : the standard printer stream, which is used for printing the data in the form hard copy from the program. The data is printed on die attached printer.

In the GNU system the shell provides the pipe and redirection facilities. With the help of these facilities the streams using the files and processes are specified. Most of the other operating systems also provide the similar mechanisms, but the details of usage may vary.

In the GNU C library, ' stdin', ‘stdout', and ' stderr' are normal variables which you can set just like others. For example, to redirect the standard output to a file, you could do:

fclose (stdout);

stdout = fopen (“output_file”, “w”);

Note however that in other systems ' stdin', ‘stdout', and ' stderr' are macros and cannot be assigned to any file in the normal way. But the function ' freopen ()' can be used to get the of closing one file and reopening another file.

Bill Smithwick

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google