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.
#include <stdio.h> extern FILE *stdin; extern FILE *stdout; extern FILE *stderr;
Try it
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.
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 tostderr. 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.
stderris unbuffered,stdoutis not, so when both go to the same file, error lines can appear before output that was printed earlier. Callfflush(stdout)first if the sequence matters, or switchstdoutto line buffering withsetvbuf(). - Redirecting inside a program is possible with
freopen(), for example to sendstdoutto a log file. Restoring the original needs OS facilities (dup/dup2on POSIX). - Reading
stdintill the end. Loop untilgetchar(),fgets()orscanf()signals end of input; seefeofandferror. 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 tostderrjust 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_FILENOand friends in<unistd.h>). - Exit status. Return
EXIT_FAILUREor a non-zero value when you wrote something tostderrbecause of a failure, so scripts can detect it. - Binary data on
stdinandstdoutis 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.
Related
printf,fprintfanddprintf,fgets,fflush,perror,freopen- Lessons: Output, User input, Command-line arguments