Input/Output Methods

The standard i/o library supports four basic types of i/o operations–character, direct, string, and formatted.

Character and direct i/o are used to read and write specific quantities of data, and treat the data they operate on as sequences of raw bytes without regards to content–this means they can be used for both plaintext and arbitrary binary data formats.

String and formatted i/o, on the other hand, are used exclusively for reading and writing plaintext data–certain byte values in a data stream have special meaning to these methods, such as whitespace characters, the null-byte '\0' string-termination character, and the '%' format-specifier character.

Each type of operation has an associated general-purpose input and output function, shown in the table below,

Operation

Input method

Output method

Description

Character

fgetc

fputc

Read or write one byte

Direct

fread

fwrite

Read or write N bytes

String

fgets

fputs

Read up to one line of input into string, or write a string.

Formatted

fscanf

fprintf

Read and interpret formatted input, or write formatted output.

To support backwards compatibility, improved performance, and feature extensions, several alternative forms of the above functions are also available; the full list is summarized below, and explained in more detail in each respective section.

#include <stdio.h>

/* Character input */
int fgetc(FILE *stream);
int getc(FILE *stream); /* May be a macro */
int getchar(void); /* Implied stdin  */

/* Character ouput */
int fputc(int c, FILE *stream);
int putc(int c, FILE *stream); /* May be a macro */
int putchar(int c); /* Implied stdout */

/* Direct input */
ssize_t fread(void *buf, size_t size, size_t nitems, FILE *stream);

/* Direct output */
ssize_t fwrite(void const *buf, size_t size, size_t nitems, FILE *stream);

/* String input */
char *fgets(char *s, int size, FILE *stream);
[[deprecated]] char *gets(char *s); /* Unsafe; never use this function */

/* String output */
int fputs(char const *s, FILE *stream);
int puts(char const *s); /* Implied stdout. Adds trailing '\n' */

/* Formatted input */
int fscanf(FILE *stream, char const *fmt, ...);
int scanf(char const *fmt, ...); /* Implied stdin */
int sscanf(char const *str, char const *fmt, ...); /* Input from string */

/* Formatted output */
int fprintf(FILE *stream, char const *fmt, ...);
int printf(char const *fmt, ...); /* Implied stdout */
int sprintf(char *str, char const *fmt, ...); /* Output to string */
int snprintf(char *str, size_t size, char const *fmt, ...); /* Bounded version of sprintf */

Each of the variadic functions listed above also have non-variadic versions, which are used when writing variadic wrapper functions,

/* Formatted input */
int vfscanf(FILE *stream, char const *fmt, va_list ap);
int vscanf(char const *fmt, va_list ap); /* Implied stdin */
int vsscanf(char const *str, char const *fmt, va_list ap); /* Input from string */

/* Formatted output */
int vfprintf(FILE *stream, char const *fmt, va_list ap);
int vprintf(char const *fmt, va_list ap); /* Implied stdout */
int vsprintf(char *str, char const *fmt, va_list ap); /* Output to string */
int vsnprintf(char *str, size_t size, char const *fmt, va_list ap); /* Bounded version of vsprintf */