You are on page 1of 3

3/13/2014 Format Codes for scanf

Ads not by this site

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.

1. Prototypes (in <stdio.h>)


int scanf(const char *format, ...)

int fscanf(FILE *stream, const char *format, ...)

int sscanf(char *string, const char *format, ...)

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

% [*] [field_width] [length_modifier] conversion_character

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

An input field is defined as a string of non-whitespace characters.

3. Assignment suppression flag, *


If the optional *flag is included, assignment is suppressed, i.e. the input field is converted according to the specification,
but not assigned to a variable. Otherwise the assignment is made to the corresponding argument, which must be a
pointer to a variable of the appropriate type.

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

h For d, i, o, uor xconversions, the value is to be input as a short, not an int.

For d, i, o, uor xconversions, the value is to be input as a long, not an int.


l
For e, for gconversions, the value is to be input as a double, not a float.

L For e, for gconversions, the value is to be input as a long double.

6. Conversion character

Character Meaning

d Input a decimal integer; argument must be int *.

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 *.

u Input an unsigned decimal integer; argument must be unsigned 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: [^]...]

% Literal %: reads the '%'character. No assignment is made.

David C. Hamill
D.Hamill@surrey.ac.uk

Last updated 11/28/2004 13:24:44

Ads not by this site

http://personal.ee.surrey.ac.uk/Personal/R.Bowden/C/scanf.html 3/3

You might also like