Skip to main content
Be Bitwise
CFile I/O

Part ofSystems Programming Companion, week 12

Standard I/O Streams

Every C program is born with three streams already open: stdin, stdout, and stderr. Almost every I/O routine in <stdio.h> is some variation on a theme played out through these three.

The Three Standard Streams

c
#include <stdio.h>
int main() {
fprintf(stdout, "This goes to standard output\n");
fprintf(stderr, "This goes to standard error\n");
char buf[100];
fgets(buf, sizeof(buf), stdin); // Read from standard input
printf("You typed: %s", buf);
return 0;
}

Under the covers, printf(...) is just fprintf(stdout, ...). scanf(...) is the same trick applied to stdin.

Streams, File Descriptors and Redirection

Underneath each FILE * sits a small integer the kernel hands out, called a file descriptor. The three standard streams always get the same ones: stdin is 0, stdout is 1 and stderr is 2. Your program never chooses where those numbers lead. The shell sets that up before main runs, and that's what redirection does.

Worked example: a program called greet reads a name with fgets, prints Hello, Ada with printf, and complains with fprintf(stderr, "warning: name has no surname\n"). Run it like this:

bash
echo Ada | ./greet > out.txt

Before greet starts, the shell wires up the three descriptors:

fdStreamConnected toWhat ends up there
0stdina pipe whose other end is echofgets reads Ada\n
1stdoutthe file out.txtHello, Ada
2stderrstill your terminalthe warning

So the terminal shows only the warning, and out.txt holds only the greeting. This is the reason for having two output streams: errors stay visible even when normal output is being saved or piped somewhere else. Add 2> err.txt to send fd 2 to a file as well, or 2>&1 to point it wherever fd 1 currently goes.

Buffering

Output streams are buffered. Hitting the kernel for every byte would be brutally slow, so libc batches things up:

  • stdout is line-buffered against a terminal — every \n triggers a flush. Pipe it into a file and the rules change to fully buffered, with much larger chunks.
  • stderr is unbuffered. Writes go through immediately, which is exactly what you want when the program is about to die.

You can pick the mode yourself with setvbuf, as long as you call it before the first read or write on the stream: setvbuf(stdout, NULL, _IONBF, 0) makes stdout unbuffered, _IOLBF line-buffers it, and _IOFBF with a size fully buffers it.

Key Concept

Buffering has a sting in the tail: if the program crashes before a buffered chunk hits disk or terminal, those bytes vanish. The classic debugging headache. Force a flush with fflush(stdout), or just print to stderr while you're hunting a bug.

Here's that headache on demand. abort() kills the process without flushing anything:

Try itRun it, then uncomment the fflush line and run it again.
C (GCC 9.2.0)

In the runner, stdout is a pipe rather than a terminal, so it's fully buffered and both printf lines die in the buffer; only the stderr line gets out. Run it in a Linux terminal and line 1 survives as well, since the newline flushed it. (macOS's abort() flushes stdio on the way down, so you'll see everything there.)

It helps to see where those bytes were sitting when the process died. Output passes through two buffers before it reaches a disk, and each hop has its own trigger:

abort() skips the first arrow entirely, which is why the runner lost both printf lines. stderr has no libc buffer at all, so its bytes go straight to the kernel.

Predict: a program runs printf("A\n"); then fprintf(stderr, "B\n"); then printf("C\n"); and returns normally. You run ./prog 2>&1 | cat. In what order do the lines appear?

B, A, C. Both streams end up in the pipe, but stdout isn't a terminal any more, so it's fully buffered: A and C wait in the buffer while B, unbuffered, goes straight through. Returning from main then flushes A and C together. Run ./prog on its own in a terminal and you get A, B, C, because each newline flushes stdout as it goes.

Character-Level I/O

Down at the byte level, getchar() and putchar() move one character at a time:

c
int ch;
while ((ch = getchar()) != EOF) {
putchar(ch);
}

getchar() returns int, not char, because EOF has to be distinguishable from every byte it could read. A byte can take all 256 values, so there's no spare one left in a char for EOF (usually -1) to use.

Formatted I/O

printf and scanf use format specifiers to handle different types:

SpecifierTypeExample
%dintprintf("%d", 42)
%sstringprintf("%s", "hello")
%ffloat/doubleprintf("%.2f", 3.14)
%ppointerprintf("%p", ptr)
%zusize_tprintf("%zu", sizeof(int))

The table is for printf. scanf reads the same letters slightly differently, because it receives addresses to write into rather than values. A float passed to printf is promoted to double, so %f covers both, but scanf needs to know exactly how many bytes to store: %f expects a float * and %lf a double *. Get that wrong and scanf writes the wrong number of bytes into your variable.

c
double price;
int qty;
if (scanf("%lf %d", &price, &qty) != 2) {
fprintf(stderr, "expected a price and a quantity\n");
return 1;
}

Note the & on each argument, and the check on the return value: scanf returns how many items it matched, so anything other than 2 means the input didn't fit the format. Reading Files shows what goes wrong when that check is missing.