Chapter 13. Input and Output

Programs must be able to write data to files or to physical output devices such as displays or printers, and to read in data from files or input devices such as a keyboard. The C standard library provides numerous functions for these purposes. This chapter presents a survey of the part of the standard library that is devoted to input and output, which is often referred to as the I/O library. Further details on the individual functions can be found in Chapter 18. Apart from these library functions, the C language itself contains no input or output support at all.

All of the basic functions, macros, and types for input and output are declared in the header file stdio.h. The corresponding input and output function declarations for wide characters of the type wchar_t are contained in the header file wchar.h.

Tip

As alternatives to the traditional standard I/O functions, C11 introduces many new functions that permit more secure programming, in particular by checking the bounds of arrays when copying data. These alternative functions have names that end with the suffix _s (such as scanf_s(), for example).

Support for these “secure” functions is optional. The macro __STDC_LIB_EXT1__ is defined in implementations that provide them (see “Functions with Bounds-Checking”).

Streams

From the point of view of a C program, all kinds of files and devices for input and output are uniformly represented as logical data streams regardless of whether the program reads ...

Get C in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.