Skip to content
Tutorials Center
My learning

Topics

C Reference ยท stdio.h

C stdin, stdout and stderr streams

Reference for the three standard streams in C: stdin, stdout and stderr. How they connect to the terminal, redirection, buffering and why errors go to stderr.

Every C program starts with three streams already open: stdin for input, stdout for normal output, and stderr for error messages. They are FILE * values declared in <stdio.h>, so every function that works on files also works on them: fprintf(stderr, ...), fgets(buf, n, stdin), fputs(s, stdout).

By default they are connected to the terminal. The shell can connect them elsewhere without any change to your program: prog < input.txt > output.txt 2> errors.txt reads from one file and sends the two output streams to different files. That separation is the reason stderr exists.

C
#include <stdio.h>

extern FILE *stdin;
extern FILE *stdout;
extern FILE *stderr;

Try it

C Using all three streams

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Error messages on stderr

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

Stream Direction Default buffering Usual meaning
stdin input line buffered on a terminal, fully buffered otherwise what the user types or a file/pipe feeding the program
stdout output line buffered on a terminal, fully buffered otherwise the program's results
stderr output unbuffered diagnostics and error messages

Shortcuts: printf(...) is fprintf(stdout, ...), puts(s) writes s and a newline to stdout, getchar() is getc(stdin), and putchar(c) is putc(c, stdout).

Errors / undefined behavior: closing a standard stream and then using it is undefined behavior. The standard does not require the names to be assignable, so write freopen("out.txt", "w", stdout) instead of stdout = fopen(...).

Usage notes

  • Results to stdout, messages to stderr. A program whose output is piped into another program should not mix error text into the data. Users can then hide or capture each stream on its own.
  • Order is not guaranteed between streams. stderr is unbuffered, stdout is not, so when both go to the same file, error lines can appear before output that was printed earlier. Call fflush(stdout) first if the sequence matters, or switch stdout to line buffering with setvbuf().
  • Redirecting inside a program is possible with freopen(), for example to send stdout to a log file. Restoring the original needs OS facilities (dup/dup2 on POSIX).
  • Reading stdin till the end. Loop until getchar(), fgets() or scanf() signals end of input; see feof and ferror. When input is piped from a file, the end arrives without any user action.
  • Interactive prompts. Print the prompt to stdout, flush it, then read. Do not print the prompt to stderr just to avoid flushing: it would still show up when stdout is redirected.
  • File descriptors. On POSIX the three streams use descriptors 0, 1 and 2 (STDIN_FILENO and friends in <unistd.h>).
  • Exit status. Return EXIT_FAILURE or a non-zero value when you wrote something to stderr because of a failure, so scripts can detect it.
  • Binary data on stdin and stdout is fine on POSIX; on Windows the streams are in text mode unless you change them.
  • Threads. The stdio functions lock each stream, so concurrent printf() calls do not corrupt the buffer, but their output can still interleave line by line.