Format Conversions:: Scanf, Fscanf, Sscanf
Format Conversions:: Scanf, Fscanf, Sscanf
Format Conversions:
scanf, fscanf, sscanf
To get started, use %hito input a short, %ifor an int, %lifor a long, %ffor a float, %lffor a double, %Lf
for a long double, %cfor a char(or %ito input it as a number) or %sfor a string (char *or char []). Then
refine the formatting further as desired.
An extremely common error is to forget that the arguments must be pointers! So if xis a float, the correct call is
scanf("%f", &x); (because &xis of type float *), not scanf("%f", x);.
For ease of debugging: if you want to input multiple values from the keyboard, do so one at a time. Each time output
a suitable prompt, then call scanfto input a single value.
The functions return the number of input values converted and assigned, or 'EOF'if the end of file is reached or an error
occurs before any conversion.
2. Format string
The format string tells scanfhow to interpret the input. It may contain:
Spaces and tabs, which are ignored but can be included for readability.
Ordinary characters which are matched against non-whitespace characters in the input. (Whitespace characters
are space ' ', tab '\t', newline '\n', carriage return '\r', vertical tab '\v'and formfeed '\f'.All others
are non-whitespace. The '%'character is a special case; see below.)
One or more conversion specifications, each of the form
where components in brackets [ ]are optional. The minimum is therefore a %and a conversion character (e.g.
%i). There is generally one conversion specification for each variable. The conversion process stops when the
format string is exhausted or when an input fails to meet the conversion specification.
http://personal.ee.surrey.ac.uk/Personal/R.Bowden/C/scanf.html 1/3
3/13/2014 Format Codes for scanf
4. Field width
An input field (a sequence of non-whitespace characters) extends either to the next whitespace character or until the
field width, if specified, is reached. (So scanfwill read across line ends to find its input, because newlines are
whitespace.)
5. Length modifier
Character Meaning
6. Conversion character
Character Meaning
Input an integer; argument must be int *. The integer may be in octal (with a leading 0) or
i
hexadecimal (with a leading 0xor 0X).
o Input an octal integer (with or without a leading 0); argument must be int *.
x Input a hexadecimal integer (with or without a leading 0xor 0X); argument must be int *.
Input one or more characters; argument must be char *. The characters are put in the character
c array, up to the number given by the field width. (No terminating '\0'is added.) If a field width is
http://personal.ee.surrey.ac.uk/Personal/R.Bowden/C/scanf.html 2/3
3/13/2014 Format Codes for scanf
given, whitespace is not skipped. If no field width is given, only one character is input. (To read the
next non-white space character, use %1s.)
Input a string of non-whitespace characters; the argument must be char *. A terminating '\0'is
s
added.
Input a floating point number; argument must be float *. The input format is an optional sign, a
string of numbers (possibly containing a decimal point), and an optional exponent field containing an
e, f, g
Eor efollowed by a (possibly signed) integer. So numbers to be input can be in either decimal or
scientific notation.
Input a pointer value; argument must be void *. The representation is implementation dependent
p
and is as printed by printf("%p").
No input is read. Instead, the argument receives the number of characters read so far by this call;
n
argument must be int *.
Inputs a string; argument must be char *. A terminating '\0'is added. Only the characters listed
[...] between the brackets are accepted. Input ends when a non-matching character is reached or the
field width is reached. To accept the ']'character, list it first: []...]
Inputs a string; argument must be char *. A terminating '\0'is added. Only characters not listed
[^...] between the brackets are accepted. Input ends when a non-matching character is reached or the
field width is reached. To reject the ']'character, list it first: [^]...]
David C. Hamill
[email protected]
http://personal.ee.surrey.ac.uk/Personal/R.Bowden/C/scanf.html 3/3