Share via


scanf Type Field Characters

The type character is the only required format field; it appears after any optional format fields. The type character determines whether the associated argument is interpreted as a character, string, or number.

Table R.8   Type Characters for scanf functions

Character Type of Input Expected Type of Argument
c When used with scanf functions, specifies single-byte character; when used with wscanf functions, specifies wide character. White-space characters that are ordinarily skipped are read when c is specified. To read next non–white-space single-byte character, use %1s; to read next non–white-space wide character, use %1ws. Pointer to char when used with scanf functions, pointer to wchar_t when used with wscanf functions.
C When used with scanf functions, specifies wide character; when used with wscanf functions, specifies single-byte character. White-space characters that are ordinarily skipped are read when C is specified. To read next non–white-space single-byte character, use %1s; to read next non–white-space wide character, use %1ws. Pointer to wchar_t when used with scanf functions, pointer to char when used with wscanf functions.
d Decimal integer. Pointer to int.
i Decimal, hexadecimal, or octal integer. Pointer to int.
o Octal integer. Pointer to int.
u Unsigned decimal integer. Pointer to unsigned int.
x Hexadecimal integer. Pointer to int.
e, E, f, g, G Floating-point value consisting of optional sign (+ or –), series of one or more decimal digits containing decimal point, and optional exponent (“e” or “E”) followed by an optionally signed integer value. Pointer to float.
n No input read from stream or buffer. Pointer to int, into which is stored number of characters successfully read from stream or buffer up to that point in current call to scanf functions or wscanf functions.
s String, up to first white-space character (space, tab or newline). To read strings not delimited by space characters, use set of square brackets ([ ]), as discussed following Table R.7. When used with scanf functions, signifies single-byte character array; when used with wscanf functions, signifies wide-character array. In either case, character array must be large enough for input field plus terminating null character, which is automatically appended.
S String, up to first white-space character (space, tab or newline). To read strings not delimited by space characters, use set of square brackets ([ ]), as discussed preceding this table. When used with scanf functions, signifies wide-character array; when used with wscanf functions, signifies single-byte–character array. In either case, character array must be large enough for input field plus terminating null character, which is automatically appended.

The types c, C, s, and S are Microsoft extensions and are not ANSI-compatible.

Thus, to read single-byte or wide characters with scanf functions and wscanf functions**,** use format specifiers as follows.

To Read Character As Use This Function With These Format Specifiers
single byte scanf functions c, hc, or hC
single byte wscanf functions C, hc, or hC
wide wscanf functions c, lc, or lC
wide scanf functions C, lc, or lC

To scan strings with scanf functions, and wscanf functions**,** use the prefixes h and l analogously with format type-specifiers s and S.