In
computer programming
Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
, standard streams are preconnected input and output
communication channel
A communication channel refers either to a physical transmission medium such as a wire, or to a logical connection over a multiplexed medium such as a radio channel in telecommunications and computer networking. A channel is used for infor ...
s between a computer program and its environment when it begins execution. The three
input/output
In computing, input/output (I/O, i/o, or informally io or IO) is the communication between an information processing system, such as a computer, and the outside world, such as another computer system, peripherals, or a human operator. Inputs a ...
(I/O) connections are called standard input (stdin), standard output (stdout) and standard error (stderr). Originally I/O happened via a physically connected
system console
A computer terminal is an electronic or electromechanical computer hardware, hardware device that can be used for entering data into, and transcribing data from, a computer or a computing system. Most early computers only had a front panel to ...
(input via keyboard, output via monitor), but standard streams abstract this. When a command is executed via an interactive
shell
Shell may refer to:
Architecture and design
* Shell (structure), a thin structure
** Concrete shell, a thin shell of concrete, usually with no interior columns or exterior buttresses
Science Biology
* Seashell, a hard outer layer of a marine ani ...
, the streams are typically connected to the
text terminal
A computer terminal is an electronic or electromechanical hardware device that can be used for entering data into, and transcribing data from, a computer or a computing system. Most early computers only had a front panel to input or display ...
on which the shell is running, but can be changed with
redirection or a
pipeline
A pipeline is a system of Pipe (fluid conveyance), pipes for long-distance transportation of a liquid or gas, typically to a market area for consumption. The latest data from 2014 gives a total of slightly less than of pipeline in 120 countries ...
. More generally, a
child process
A child process (CP) in computing is a process created by another process (the parent process). This technique pertains to multitasking operating systems, and is sometimes called a subprocess or traditionally a subtask.
There are two major proce ...
inherits the standard streams of its
parent process
In computing, a parent process is a process that has created one or more child processes.
Unix-like systems
In Unix-like operating systems, every process except (the swapper) is created when another process executes the fork() system call. T ...
.
Application

Users generally know standard streams as input and output channels that handle data coming from an input device, or that write data from the application. The data may be text with any encoding, or
binary data
Binary data is data whose unit can take on only two possible states. These are often labelled as 0 and 1 in accordance with the binary numeral system and Boolean algebra.
Binary data occurs in many different technical and scientific fields, wh ...
.
When a program is run as a
daemon
A demon is a malevolent supernatural being, evil spirit or fiend in religion, occultism, literature, fiction, mythology and folklore.
Demon, daemon or dæmon may also refer to:
Entertainment Fictional entities
* Daemon (G.I. Joe), a character ...
, its standard error stream is redirected into a log file, typically for error analysis purposes.
Streams may be used to chain applications, meaning that the output stream of one program can be redirected to be the input stream to another application. In many operating systems this is expressed by listing the application names, separated by the vertical bar character, for this reason often called the
pipeline
A pipeline is a system of Pipe (fluid conveyance), pipes for long-distance transportation of a liquid or gas, typically to a market area for consumption. The latest data from 2014 gives a total of slightly less than of pipeline in 120 countries ...
character. A well-known example is the use of a
pagination
Pagination, also known as paging, is the process of dividing a document into discrete page (paper), pages, either electronic pages or printed pages.
In reference to books produced without a computer, pagination can mean the consecutive page num ...
application, such as
more, providing the user control over the display of the output stream on the display.
Background
In most operating systems predating
Unix
Unix (, ; trademarked as UNIX) is a family of multitasking, multi-user computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, a ...
, programs had to explicitly connect to the appropriate input and output devices. OS-specific intricacies caused this to be a tedious programming task. On many systems it was necessary to obtain control of environment settings, access a local file table, determine the intended data set, and handle hardware correctly in the case of a
punch card reader,
magnetic tape drive,
disk drive,
line printer
A line printer Printer (computing), prints one entire line of text before advancing to another line. Most early line printers were
printer (computing)#Impact printers, impact printers.
Line printers are mostly associated with unit record eq ...
, card punch, or interactive terminal.
One of Unix's several groundbreaking advances was ''abstract devices'', which removed the need for a program to know or care what kind of devices it was communicating with. Older operating systems forced upon the programmer a record structure and frequently
non-orthogonal data semantics and device control. Unix eliminated this complexity with the concept of a data stream: an ordered sequence of data bytes which can be read until the
end of file. A program may also write bytes as desired and need not, and cannot easily declare their count or grouping.
Another Unix breakthrough was to automatically associate input and output to terminal keyboard and terminal display, respectively, by default — the program (and programmer) did absolutely nothing to establish input and output for a typical input-process-output program (unless it chose a different paradigm). In contrast, previous operating systems usually required some—often complex—
job control language
Job Control Language (JCL) is a scripting language used on IBM mainframe operating systems to instruct the system on how to run a batch processing, batch job or start a subsystem. The purpose of JCL is to say which programs to run, using which fi ...
to establish connections, or the equivalent burden had to be orchestrated by the program.
Since Unix provided standard streams, the Unix
C runtime environment was obliged to support it as well. As a result, most C runtime environments (and
C's descendants), regardless of the operating system, provide equivalent functionality.
Standard input (stdin)
Standard input is a stream from which a program reads its input data. The program requests data transfers by use of the ''read'' operation. Not all programs require stream input. For example, the ''
dir'' and ''
ls'' programs (which display file names contained in a directory) may take
command-line arguments, but perform their operations without any stream data input.
Unless
redirected, standard input is inherited from the parent process. In the case of an interactive shell, that is usually associated with the input device of a
terminal (or
pseudo terminal
In some operating systems, including Unix-like systems, a pseudoterminal, pseudotty, or PTY is a pair of pseudo-device endpoints (files) which establish an asynchronous, Duplex (telecommunications), bidirectional communication (IPC socket, IPC) c ...
) which is ultimately linked to a user's
keyboard.
On
POSIX
The Portable Operating System Interface (POSIX; ) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines application programming interfaces (APIs), along with comm ...
systems, the
file descriptor
In Unix and Unix-like computer operating systems, a file descriptor (FD, less frequently fildes) is a process-unique identifier (handle) for a file or other input/output resource, such as a pipe or network socket.
File descriptors typically h ...
for standard input is 0 (zero); the
POSIX
The Portable Operating System Interface (POSIX; ) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines application programming interfaces (APIs), along with comm ...
definition is
STDIN_FILENO
; the corresponding C
abstraction is provided via the
FILE* stdin
global variable. Similarly, the global C++
std::cin
variable of type
provides an abstraction via
C++ streams. Similar abstractions exist in the standard I/O libraries of practically every
programming language
A programming language is a system of notation for writing computer programs.
Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
.
Standard output (stdout)
Standard output is a stream to which a program writes its output data. The program requests data transfer with the ''write'' operation. Not all programs generate output. For example, the ''
file rename'' command (variously called ''
mv'', ''
move
Move or The Move may refer to:
Brands and enterprises
* Move (company), an American online real estate company
* Move (electronics store), a defunct Australian electronics retailer
* Daihatsu Move, a Japanese car
* PlayStation Move, a motion ...
'', or ''
ren'') is silent on success.
Unless
redirected, standard output is inherited from the parent process. In the case of an interactive shell, that is usually the
text terminal
A computer terminal is an electronic or electromechanical hardware device that can be used for entering data into, and transcribing data from, a computer or a computing system. Most early computers only had a front panel to input or display ...
which initiated the program.
The
file descriptor
In Unix and Unix-like computer operating systems, a file descriptor (FD, less frequently fildes) is a process-unique identifier (handle) for a file or other input/output resource, such as a pipe or network socket.
File descriptors typically h ...
for standard output is 1 (one); the
POSIX
The Portable Operating System Interface (POSIX; ) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines application programming interfaces (APIs), along with comm ...
definition is
STDOUT_FILENO
; the corresponding C
variable is
FILE* stdout
; similarly, the C++
variable is
std::cout
.
Standard error (stderr)
Standard error is another output stream typically used by programs to output
error messages or diagnostics. It is a stream independent of standard output and can be redirected separately.
This solves the
semi-predicate problem, allowing output and errors to be distinguished, and is analogous to a function returning a pair of values – see . The usual destination is the
text terminal
A computer terminal is an electronic or electromechanical hardware device that can be used for entering data into, and transcribing data from, a computer or a computing system. Most early computers only had a front panel to input or display ...
which started the program to provide the best chance of being seen even if ''standard output'' is redirected (so not readily observed). For example, output of a program in a
pipeline
A pipeline is a system of Pipe (fluid conveyance), pipes for long-distance transportation of a liquid or gas, typically to a market area for consumption. The latest data from 2014 gives a total of slightly less than of pipeline in 120 countries ...
is redirected to input of the next program or a text file, but errors from each program still go directly to the text terminal so they can be reviewed by the user in real time.
It is acceptable and normal to direct ''standard output'' and ''standard error'' to the same destination, such as the text terminal. Messages appear in the same order as the program writes them, unless
buffering is involved. For example, in common situations the standard error stream is unbuffered but the standard output stream is line-buffered; in this case, text written to standard error later may appear on the terminal earlier, if the standard output stream buffer is not yet full.
The
file descriptor
In Unix and Unix-like computer operating systems, a file descriptor (FD, less frequently fildes) is a process-unique identifier (handle) for a file or other input/output resource, such as a pipe or network socket.
File descriptors typically h ...
for standard error is defined by
POSIX
The Portable Operating System Interface (POSIX; ) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines application programming interfaces (APIs), along with comm ...
as 2 (two); the ''<unistd.h>'' header file provides the symbol
STDERR_FILENO
; the corresponding C
variable is
FILE* stderr
. The C++
standard header provides two variables associated with this stream:
std::cerr
and
std::clog
, the former being unbuffered and the latter using the same buffering mechanism as all other C++ streams.
Bourne-style shells allow ''standard error'' to be redirected to the same destination that standard output is directed to using
2>&1
csh-style shells allow ''standard error'' to be redirected to the same destination that standard output is directed to using
>&
Standard error was added to Unix in the 1970s after several wasted phototypesetting runs ended with error messages being typeset instead of displayed on the user's terminal.
Timeline
1950s: Fortran
Fortran has the equivalent of Unix file descriptors: By convention, many Fortran implementations use unit numbers
UNIT=5
for stdin,
UNIT=6
for stdout and
UNIT=0
for stderr. In Fortran-2003, the intrinsic
ISO_FORTRAN_ENV
module was standardized to include the named constants
INPUT_UNIT
,
OUTPUT_UNIT
, and
ERROR_UNIT
to portably specify the unit numbers.
! FORTRAN 77 example
PROGRAM MAIN
INTEGER NUMBER
READ(UNIT=5,*) NUMBER
WRITE(UNIT=6,'(A,I3)') ' NUMBER IS: ',NUMBER
END
! Fortran 2003 example
program main
use iso_fortran_env
implicit none
integer :: number
read (unit=INPUT_UNIT,*) number
write (unit=OUTPUT_UNIT,'(a,i3)') 'Number is: ', number
end program
1960: ALGOL 60
ALGOL 60
ALGOL 60 (short for ''Algorithmic Language 1960'') is a member of the ALGOL family of computer programming languages. It followed on from ALGOL 58 which had introduced code blocks and the begin and end pairs for delimiting them, representing a ...
was criticized for having no standard file access.
1968: ALGOL 68
ALGOL 68
ALGOL 68 (short for ''Algorithmic Language 1968'') is an imperative programming language member of the ALGOL family that was conceived as a successor to the ALGOL 60 language, designed with the goal of a much wider scope of application and ...
's input and output facilities were collectively referred to as the transput.
Koster coordinated the definition of the ''transput'' standard. The model included three standard channels:
stand in
,
stand out
, and
stand back
.
1970s: C and Unix
In the
C programming language
C (''pronounced'' '' – like the letter c'') is a general-purpose programming language. It was created in the 1970s by Dennis Ritchie and remains very widely used and influential. By design, C's features cleanly reflect the capabilities of ...
, the standard input, output, and error streams are attached to the existing Unix file descriptors 0, 1 and 2 respectively. In a
POSIX
The Portable Operating System Interface (POSIX; ) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines application programming interfaces (APIs), along with comm ...
environment the ''<
unistd.h
In the C programming language, C and C++ programming languages, unistd.h is the name of the header file that provides access to the POSIX operating system application programming interface, API. It is defined by the POSIX.1 standard, the base ...
>'' definitions ''STDIN_FILENO'', ''STDOUT_FILENO'' or ''STDERR_FILENO'' should be used instead rather than
magic numbers. File pointers ''stdin'', ''stdout'', and ''stderr'' are also provided.
Ken Thompson
Kenneth Lane Thompson (born February 4, 1943) is an American pioneer of computer science. Thompson worked at Bell Labs for most of his career where he designed and implemented the original Unix operating system. He also invented the B (programmi ...
(designer and implementer of the original Unix operating system) modified
sort in
Version 5 Unix to accept "-" as representing standard input, which spread to other utilities and became a part of the operating system as a
special file in
Version 8. Diagnostics were part of standard output through
Version 6, after which
Dennis M. Ritchie created the concept of standard error.
1995: Java
In
Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
, the standard streams are referred to by (for stdin), (for stdout), and (for stderr).
public static void main(String args[])
2000s: .NET
In C Sharp (programming language), C# and other .NET Framework, .NET languages, the standard streams are referred to by
System.Console.In
(for stdin),
System.Console.Out
(for stdout) and
System.Console.Error
(for stderr). Basic read and write capabilities for the stdin and stdout streams are also accessible directly through the class
System.Console
(e.g.
System.Console.WriteLine()
can be used instead of
System.Console.Out.WriteLine()
).
System.Console.In
,
System.Console.Out
and
System.Console.Error
are
System.IO.TextReader
(stdin) and
System.IO.TextWriter
(stdout, stderr) objects, which only allow access to the underlying standard streams on a text basis. Full binary access to the standard streams must be performed through the
System.IO.Stream
objects returned by
System.Console.OpenStandardInput()
,
System.Console.OpenStandardOutput()
and
System.Console.OpenStandardError()
respectively.
// C# example
public static int Main(string[] args)
' Visual Basic .NET example
Public Function Main() As Integer
Try
Dim s As String = System.Console.[In].ReadLine()
Dim number As Double = Double.Parse(s)
System.Console.Out.WriteLine("Number is: ", number)
Return 0
' If Parse() threw an exception
Catch ex As System.ArgumentNullException
System.Console. rrorWriteLine("No number was entered!")
Catch ex2 As System.FormatException
System.Console. rrorWriteLine("The specified value is not a valid number!")
Catch ex3 As System.OverflowException
System.Console. rrorWriteLine("The specified number is too big!")
End Try
Return -1
End Function
When applying the
System.Diagnostics.Process
class
Class, Classes, or The Class may refer to:
Common uses not otherwise categorized
* Class (biology), a taxonomic rank
* Class (knowledge representation), a collection of individuals or objects
* Class (philosophy), an analytical concept used d ...
one can use the instance
properties
Property is the ownership of land, resources, improvements or other tangible objects, or intellectual property.
Property may also refer to:
Philosophy and science
* Property (philosophy), in philosophy and logic, an abstraction characterizing an ...
StandardInput
,
StandardOutput
, and
StandardError
of that class to access the standard streams of the process.
2000 - : Python (2 or 3)
The following example, written in
Python, shows how to redirect the standard input both to the standard output
and to a text file.
#!/usr/bin/env python
import sys
# Save the current stdout so that we can revert sys.stdout
# after we complete our redirection
stdin_fileno = sys.stdin
stdout_fileno = sys.stdout
# Redirect sys.stdout to the file
sys.stdout = open("myfile.txt", "w")
ctr = 0
for inps in stdin_fileno:
ctrs = str(ctr)
# Prints to the redirected stdout ()
sys.stdout.write(ctrs + ") this is to the redirected --->" + inps + "\n")
# Prints to the actual saved stdout handler
stdout_fileno.write(ctrs + ") this is to the actual --->" + inps + "\n")
ctr = ctr + 1
# Close the file
sys.stdout.close()
# Restore sys.stdout to our old saved file handler
sys.stdout = stdout_fileno
GUIs
Graphical user interface
A graphical user interface, or GUI, is a form of user interface that allows user (computing), users to human–computer interaction, interact with electronic devices through Graphics, graphical icon (computing), icons and visual indicators such ...
s (GUIs) do not always make use of the standard streams; they do when GUIs are wrappers of underlying scripts and/or console programs, for instance the
Synaptic package manager GUI, which wraps apt commands in Debian and/or Ubuntu. GUIs created with scripting tools like Zenity and KDialog by
KDE
KDE is an international free software community that develops free and open-source software. As a central development hub, it provides tools and resources that enable collaborative work on its projects. Its products include the KDE Plasma gra ...
project
make use of stdin, stdout, and stderr, and are based on simple scripts rather than a complete GUI programmed and compiled in C/C++ using
Qt,
GTK
GTK (formerly GIMP ToolKit and GTK+) is a free software cross-platform widget toolkit for creating graphical user interfaces (GUIs). It is licensed under the terms of the GNU Lesser General Public License, allowing both Free software, free and ...
, or other equivalent proprietary widget framework.
The
Services menu
The Services menu (or simply Services) is a user interface element in macOS. The services are programs that accept input from the user selection, process it, and optionally put the result back in the clipboard. The concept originated in the NeXTST ...
, as implemented on
NeXTSTEP
NeXTSTEP is a discontinued object-oriented, multitasking operating system based on the Mach kernel and the UNIX-derived BSD. It was developed by NeXT, founded by Steve Jobs, in the late 1980s and early 1990s and was initially used for its ...
and
Mac OS X
macOS, previously OS X and originally Mac OS X, is a Unix, Unix-based operating system developed and marketed by Apple Inc., Apple since 2001. It is the current operating system for Apple's Mac (computer), Mac computers. With ...
, is also analogous to standard streams. On these operating systems, graphical applications can provide functionality through a system-wide menu that operates on the current
selection
Selection may refer to:
Science
* Selection (biology), also called natural selection, selection in evolution
** Sex selection, in genetics
** Mate selection, in mating
** Sexual selection in humans, in human sexuality
** Human mating strat ...
in the GUI, no matter in what application.
Some GUI programs, primarily on Unix, still write debug information to standard error. Others (such as many Unix media players) may read files from standard input. Popular Windows programs that open a separate console window in addition to their GUI windows are the emulators
pSX and
DOSBox
DOSBox is a free and open-source MS-DOS emulator. It supports running programs primarily video games that are otherwise inaccessible since hardware for running a compatible disk operating system (DOS) is obsolete and generally unavailab ...
.
GTK-server can use stdin as a communication interface with an interpreted program to realize a GUI.
The
Common Lisp Interface Manager paradigm "presents" GUI elements sent to an extended output stream.
See also
*
Redirection (computing)
In computing, redirection is a form of interprocess communication, and is a function common to most command-line interpreters, including the various Unix shells that can redirect standard streams to user-specified locations. The concept of red ...
*
Stream (computing)
In computer science, a stream is a sequence of potentially unlimited data elements made available over time. A stream can be thought of as items on a conveyor belt being processed one at a time rather than in large batches. Streams are processe ...
*
Input/output
In computing, input/output (I/O, i/o, or informally io or IO) is the communication between an information processing system, such as a computer, and the outside world, such as another computer system, peripherals, or a human operator. Inputs a ...
*
C file input/output
*
SYSIN and
SYSOUT
*
Standard streams in the Files-11 file system
References
Sources
*
Standard Streams* ''KRONOS 2.1 Reference Manual'', Control Data Corporation, Part Number 60407000, 1974
* ''NOS Version 1 Applications Programmer's Instant'', Control Data Corporation, Part Number 60436000, 1978
Level 68 Introduction to Programming on MULTICS, Honeywell Corporation, 1981
Evolution of the MVS Operating System IBM Corporation, 1981
* ''Lions' Commentary on UNIX Sixth Edition'', John Lions, , 1977
Console Class, .NET Framework Class Library Microsoft Corporation, 2008
External links
- by The Linux Information Project
- by The Linux Information Project
- by The Linux Information Project
{{DEFAULTSORT:Standard Streams
Unix