C Language Streams Explained

Stream Definition

For each C program, all I/O operations simply transfer bytes into and out of the program. This sequence of bytes is called a stream. Most streams are fully buffered, meaning reads and writes copy data to/from a memory segment called a buffer—memory-to-memory transfers are extreme fast. Output buffers are only flushed to the target device or file when full; sending a full buffer at once is far more efficient than sending small chunks repeatedly.

The fflush function forces immediate buffer flushing regardless of its fill state.

Stream Types

Streams fall into two categories:

  • Text streams: Data flows as characters, with \n sequences handling newlines according to platform conventions.
  • Binary streams: Data flows as raw binary digits. Characters are stored as single-byte ASCII binary, and numbers retain they binary representations (\n is unmodified). For example, the integer 2001 occupies 4 bytes in a text stream (ASCII values 50, 48, 48, 49 for '2','0','0','1') but only 2 bytes in a binary stream (hex 07D1, binary 00000111 11010001).

Stream Overview

Data Handling Target Input Function Output Function Description Return Type
Single Character getchar putchar Reads/writes one character int (to accommodate EOF, defined as -1)
Unformatted Line gets puts Handles raw text line I/O char* (pointer to the processed line)
Formatted Line scanf printf Handles structured line I/O with format codes int (count of converted values, or EOF for errors/end-of-input during initial reads)
Binary Data fread fwrite Reads/writes raw binary blocks size_t (number of successful block transfers)

Formatted Input Functions

The scanf family reads input from various sources, converts it using format codes, and stops when the format string ends or input no longer matches. The number of successfully converted values is returned; EOF is returned if end-of-file is encountered before any conversions.

// Three main scanf variants with different input sources
int standard_scan(const char *format, ...);  // Reads from standard input (e.g., keyboard)
int file_scan(FILE *target_stream, const char *format, ...);  // Reads from a specific FILE* stream
int string_scan(const char *input_str, const char *format, ...);  // Reads from a null-terminated string

// Use %n to retrieve the total character count read so far—it consumes no input and isn’t counted in the return value

Formatted Output Functions

The printf family generates structured output using format codes.

// Three main printf variants with different output destinations
int file_print(FILE *target_stream, const char *format, ...);  // Writes to a specific FILE* stream
int standard_print(const char *format, ...);  // Writes to standard output
int string_build(char *output_buffer, const char *format, ...);  // Writes a null-terminated string to a buffer

// Format code structure breakdown: optional left/right alignment flag (- for left, default right), width specifier, optional precision (after .) for rounding or padding

Tags: C Language I/O Streams Buffered I/O Text vs Binary Streams Formatted I/O

Posted on Mon, 31 Aug 2026 16:42:44 +0000 by not_skeletor