Node:Formatted input conversion specifiers, Previous:sscanf, Up:Formatted string input



Formatted input conversion specifiers

c
Matches a fixed number of characters. If you specify a maximum field width (see below), that is how many characters will be matched; otherwise, %c matches one character. This conversion does not append a null character to the end of the text it reads, as does the %s conversion. It also does not skip whitespace characters, but reads precisely the number of characters it was told to, or generates a matching error if it cannot.
d
Matches an optionally signed decimal integer, containing the following sequence:

  1. An optional plus or minus sign (+ or -).
  2. One or more decimal digits.

Note that %d and %i are not synonymous for scanf, as they are for printf.

e
Matches an optionally signed floating-point number, containing the following sequence:

  1. An optional plus or minus sign (+ or -).
  2. A floating-point number in decimal or hexadecimal format.
    • The decimal format is a sequence of one or more decimal digits, optionally containing a decimal point character (usually .), followed by an optional exponent part, consisting of a character e or E, an optional plus or minus sign, and a sequence of decimal digits.
    • The hexadecimal format is a 0x or 0X, followed by a sequence of one or more hexadecimal digits, optionally containing a decimal point character, followed by an optional binary-exponent part, consisting of a character p or P, an optional plus or minus sign, and a sequence of digits.

E
Same as e.
f
Same as e.
g
Same as e.
G
Same as e.
i
Matches an optionally signed integer, containing the following sequence:
  1. An optional plus or minus sign (+ or -).
  2. A string of characters representing an unsigned integer.
    • If the string begins with 0x or 0X, the number is assumed to be in hexadecimal format, and the rest of the string must contain hexadecimal digits.
    • Otherwise, if the string begins with 0, the number is assumed to be in octal format (base eight), and the rest of the string must contain octal digits.
    • Otherwise, the number is assumed to be in decimal format, and the rest of the string must contain decimal digits.

Note that %d and %i are not synonymous for scanf, as they are for printf. You can print integers in this syntax with printf by using the # flag character with the %x or %d output conversions. (See printf.)

s
Matches a string of non-whitespace characters. It skips initial whitespace, but stops when it meets more whitespace after it has read something. It stores a null character at the end of the text that it reads, to mark the end of the string. (See String overflows with scanf, for a warning about using this conversion.)
x
Matches an unsigned integer in hexadecimal format. The string matched must begin with 0x or 0X, and the rest of the string must contain hexadecimal digits.
X
Same as x.
[
Matches a string containing an arbitrary set of characters. For example, %12[0123456789] means to read a string with a maximum field width of 12, containing characters from the set 0123456789 -- in other words, twelve decimal digits. An embedded - character means a range of characters; thus %12[0-9] means the same thing as the last example. Preceding the characters in the square brackets with a caret (^) means to read a string not containing the characters listed. Thus, %12[^0-9] means to read a twelve-character string not containing any decimal digit. (See String overflows with scanf, for a warning about using this conversion.)
%
Matches a percent sign. Does not correspond to an argument, and does not permit flags, field width, or type modifier to be specified (see below).

In between the percent sign (%) and the input conversion character, you can place some combination of the following modifiers, in sequence. (Note that the percent sign conversion (%%) doesn't use arguments or modifiers.)