C - Input Format Control Strings

Introduction

Conversion Characters and Their Meanings

Conversion
character
Meaning

d
Reads the input as a signed decimal integer.
i


Reads the input as a signed integer. If the input begins with 0, then the input is assumed to be
octal. If the input starts with 0x or 0X, then hexadecimal input is assumed; otherwise decimal
input is assumed.
o
Reads the input assuming it is a signed octal integer.
u
Reads the input as an unsigned integer.
x
Reads the input as a signed hexadecimal integer.
c



Reads the number of characters specified by the field width as type char, including
whitespace. If the field width specification is absent, one character will be read. If you want to
ignore preceding whitespace when reading characters, precede the format specification by
a whitespace character.
s

Reads a string of successive non white space characters as type char, starting with the next
non white space character.
[]





Reads characters from the specified set between the square brackets. The first character not
found in the set ends the input. For example, the specification %[ab] will only read successive
characters that are a or b. To include a ']' character in the set, it must be the first character
following the initial left square bracket. Thus %[]ab] reads characters as long as they are a, b,
or ]. If the first character after [ is ^, then characters are read if they were not in the set that
follows. The specification %[^]ab] reads characters as long as they are not a, b, or ].
a, A, e, E,
f, F, G, or g
Converts the input to type float. A sign, a decimal point, and an exponent in the input
are optional.
%
Reads a % character that is not stored. Thus the specification to skip a % character is %%.
p

Reads input as a pointer. The argument should be of type void**. The input form is
implementation defined, so consult your compiler and library documentation.
n



No input is read, but the number of characters that have been read from the keyboard up to
this point are stored in the memory pointed to by the corresponding argument, which should
be of type int*. Reading with this specification does not increment the value of the assignment
count that is returned.

Note

The following three specification:

C  s []

Two arguments are required.

  • a pointer to type char and
  • the number of bytes pointed to.

If the l length modifier is present, the characters are converted to wchar_t.

Any non white space character other than % will cause scanf_s() to read but not store successive occurrences of the character.

To ignore commas separating values in the input, precede each format specifier by a comma.

The % indicates the start of the format specifier. It must always be present.