Processes in Unix-like operating systems (Linux, macOS, WSL, etc.) communicate with the outside world using three special data streams: standard input (stdin), standard output (stdout), and standard error (stderr). If you’re coming from a C# or Windows background, these might seem a bit mysterious at first. This article will explain what they are, how they work, why they matter, and how you use them in both C and C#.
Every program that runs on a Unix-like system is automatically given three "channels" by the operating system:
- stdin (standard input): Where the program gets its input data (usually from the keyboard).
- stdout (standard output): Where the program sends its normal output (usually to the terminal window).
- stderr (standard error): Where the program sends error messages and warnings (also to the terminal window by default).
These are not just variables or places in memory—they are special file streams (sometimes called file descriptors or file handles) that your program can read from and write to. The OS sets these up for you behind the scenes.
Imagine your program is a person in a room:
- stdin is like a mailbox outside the door. People can put letters (input) into it, and your program reads them when it wants input.
- stdout is like a loudspeaker. Whenever your program wants to speak (normal output), it broadcasts through this loudspeaker so everyone in the room (the terminal) can hear.
- stderr is like a separate emergency loudspeaker, only for warnings and errors—so even if people are ignoring the normal loudspeaker, they’ll notice urgent messages.
If you’ve written console programs in C#, you’ve already used these ideas:
| Name | C# Equivalent | Used For |
|---|---|---|
| stdin | Console.ReadLine() |
User input |
| stdout | Console.WriteLine() |
Normal output |
| stderr | Console.Error.WriteLine() |
Error output |
Console.ReadLine()reads from stdin (mailbox).Console.WriteLine()writes to stdout (loudspeaker).Console.Error.WriteLine()writes to stderr (emergency loudspeaker).
In C#, these are abstracted as part of the Console class, but the underlying streams are the same as in C or Unix.
- In C, they are global variables (type
FILE*) namedstdin,stdout, andstderrprovided by<stdio.h>. - In the OS, they are file descriptors:
stdin= file descriptor 0stdout= file descriptor 1stderr= file descriptor 2
By default, these pipes connect to your terminal, but you (or the user) can redirect them to files or other programs.
#include <stdio.h>
int main() {
char name[100];
fprintf(stdout, "Enter your name: "); // write to stdout (same as printf)
fscanf(stdin, "%99s", name); // read from stdin (same as scanf)
fprintf(stderr, "Hello, %s!\n", name); // write to stderr
return 0;
}printf()is just a shortcut forfprintf(stdout, ...).scanf()is just a shortcut forfscanf(stdin, ...).fprintf(stderr, ...)writes directly to error output.
using System;
class Program {
static void Main() {
Console.Write("Enter your name: "); // stdout
string name = Console.ReadLine(); // stdin
Console.Error.WriteLine($"Hello, {name}!"); // stderr
}
}- stdout is for the normal output of your program (what the user asked for).
- stderr is for error messages (problems, warnings, etc.).
- Keeping them separate means error messages don’t get mixed up with your program’s real output, which is great for automation and scripting.
Unix-like systems let you redirect these streams:
| Symbol | What it does | Example usage |
|---|---|---|
> |
Redirect stdout (output) to a file | dotnet run > output.txt |
< |
Redirect stdin (input) from a file | dotnet run < input.txt |
2> |
Redirect stderr (error output) to a file | dotnet run 2> errors.txt |
>> |
Append stdout to a file (instead of overwriting it) | dotnet run >> output.txt |
2>&1 |
Redirect stderr to wherever stdout is going (combine them) | dotnet run > all.txt 2>&1 |
-
dotnet run > output.txt
All output fromConsole.WriteLine()goes into the fileoutput.txtinstead of your terminal. -
dotnet run 2> errors.txt
All output fromConsole.Error.WriteLine()goes intoerrors.txt. Normal output still appears in your terminal. -
dotnet run > output.txt 2>&1
Both normal output and error output are combined and written tooutput.txt. -
dotnet run < input.txt
The program reads its input (whatConsole.ReadLine()receives) from the fileinput.txtinstead of your keyboard. -
dotnet run | grep error
The output from your program is piped to thegrepcommand, which searches for lines containing "error". This is how you can chain programs together!
+---------------------------+
| Your Program |
+---------------------------+
| stdin | stdout | stderr |
+------------------------+
| | |
[mailbox][loudspeaker][emergency loudspeaker]
But with redirection, you can do:
+---------------------------+
| Your Program |
+---------------------------+
| stdin | stdout | stderr |
+---------------------------+
| | |
[input.txt][output.txt][errors.txt]
| Stream | File Descriptor | Default Destination | Typical Use |
|---|---|---|---|
| stdin | 0 | Keyboard | User/program input |
| stdout | 1 | Terminal | Usual output |
| stderr | 2 | Terminal | Error messages |
- They are special global variables (C) or objects (C#) that represent system-provided streams, not ordinary variables you declare.
- Yes! Wherever you need to read input or write output, use these symbols. They make your program much more flexible and scriptable.
- Absolutely. This is one of the key powers of Unix-style programming.
- C: Use functions like
fscanf(stdin, ...),fprintf(stdout, ...), orfprintf(stderr, ...). - C#: Use
Console.ReadLine(),Console.WriteLine(), andConsole.Error.WriteLine().
Understanding stdin, stdout, and stderr is key to writing programs that "play nicely" with the Unix/Linux shell and other command-line tools. Whether you’re coding in C, C#, or another language, these streams give you powerful ways to handle input and output — and to make your programs more useful in scripts, automation, and larger systems.
If you’re coming from a Windows or C# background, just remember: these streams are the foundation of how almost all command-line programs communicate. Once you get comfortable with them, you’ll be able to take full advantage of everything the Unix environment has to offer!