C - Input Format Specifications

Introduction

The input functions have the following prototypes:

int scanf(const char* restrict format, ...);
int scanf_s(const char* restrict format, ...);
int vscanf(const char* restrict format, va_list arg);
int vscanf_s(const char* restrict format, va_list arg);
int sscanf(const char* restrict source, const char* restrict format, ...);
int sscanf_s(const char* restrict source, const char* restrict format, ...);
int vsscanf(const char* restrict source, const char* restrict format, va_list arg);
int vsscanf_s(const char* restrict source, const char* restrict format, va_list arg);
int fscanf(FILE* restrict stream, const char* restrict format, ...);
int fscanf_s(FILE* restrict stream, const char* restrict format, ...);
int vfscanf(FILE* restrict stream, const char* restrict format, va_list arg);
int vfscanf_s(FILE* restrict stream, const char* restrict format, va_list arg);

The functions with names ending _s are optional safe bounds checking versions.

It requires that __STDC_WANT_LIB_EXT1__ be defined as 1.

These functions returns a count of the number of data items read by the operation.

The ellipsis indicates that there can be zero or more arguments here.

The secure functions require that two arguments are supplied for c, s, and [ type specifiers, in which case the first must be a pointer and the second must be a value of type size_t.

The format specification for an item of data is of the form:

%[*][width][size_flag]type

The items enclosed between square brackets are optional.

The mandatory parts are the % character marking the start of the format specification and the type conversion type specification at the end.

The choices for the optional parts are:

[*] indicates that the input data item corresponding to this format specification should be scanned but not stored. For example, %*d will scan an integer value and discard it.

[width] specifies the maximum number of characters to be scanned for this input value. For example, %2d reads up to two characters as an integer value.

[size_flag] modifies the input type specified by the type part of the specification. Possible size_flag specifications are:

Flag
Meaning
l (lowercase L) a d, i, u, o, x, X, or n conversion type applies to an argument
of type long* or unsigned long*. When applied to a type a, A, e, E, f, F, g, or G conversion,
the argument is type double*. When applied to a type c, s, or [ conversion, the argument
is type wchar_t*.
L
specifies that a following floating-point conversion specifier applies to a long double* argument.
ll
specifies that a d, i, u, o, x, X, or n conversion type applies to an argument of type long long* or unsigned long long*.
h
specifies that a d, i, u, o, x, X, or n conversion type applies to an argument of type short* or unsigned short*.
hh
specifies that a d, i, u, o, x, X, or n conversion type applies to an argument of type signed char* or unsigned char*.
j
specifies that a following d, i, u, o, x, X, or n conversion type applies to an argument of type intmax_t* or uintmax_t*.
z
specifies that a following d, i, u, o, x, X, or n conversion type applies to an argument of type size_t*.
t
specifies that a following d, i, u, o, x, X, or n conversion type applies to an argument of type ptrdiff_t*.

type specifies the type of data conversion and can be any of the following:

Type Meaning
c reads a single character as type char.
d or ireads successive decimal digits as a value of type int.
u reads successive decimal digits as a value of type unsigned int.
o reads successive octal digits as a value of type unsigned int.
x or X reads successive hexadecimal digits as a value of type unsigned int.
a, A, e, E, f, F, g, or G reads an optionally signed floating-point value as a value of type float.
s reads successive characters until a whitespace is reached and stores the characters read in the buffer pointed to by the corresponding argument.
preads the input as a pointer value. The corresponding argument must be of type void**.
% matches a single % character in the input that is not stored.
nNo input is read but the number of characters that have been read from the input source up to this point is stored in the corresponding argument, which should be of type int*.

To read a string that includes whitespace characters, use %[set_of_characters].

%[abcdefghijklmnopqrstuvwxyz] will read any sequence of lowercase letters and spaces as a single string.

%[^,!] will read a sequence of characters until either a comma or an exclamation point is found, which will end the string input.

^ does the negation.