scanf() STDIO scanf()
Accept and format input
#include <stdio.h>
int scanf(format, arg1, ... argN)
char *format; [data type] *arg1, ... *argN;
scanf reads the standard input, and uses the string format to
specify a format for each arg1 through argN, each of which must
be a pointer.
scanf reads one character at a time from format; white space
characters are ignored. The percent sign character `%' marks the
beginning of a conversion specification. `%' may be followed by
characters that indicate the width of the input field and the
type of conversion to be done.
scanf reads the standard input until the return key is pressed.
Inappropriate characters are thrown away; e.g., it will not try
to write an alphabetic character into an int.
The following modifiers can be used within the conversion string:
1. The asterisk `*', which indicates that the next input field
should be skipped rather than assigned to the next arg.
2. A string of decimal digits, which specifies a maximum field
width.
3. An l, which specifies that the next input item is a long ob-
ject rather than an int object. Capitalizing the conversion
character has the same effect.
The following conversion specifiers are recognized:
c Assign the next input character to the next arg, which should
be of type char *.
d Assign the decimal integer from the next input field to the
next arg, which should be of type int *.
D Assign the decimal integer from the next input field to the
next arg, which should be of type long *.
e Assign the floating point number from the next input field to
the next arg, which should be of type float *.
E Assign the floating point number from the next input field to
the next arg, which should be of type double *.
f Same as e.
F Same as E.
COHERENT Lexicon Page 1
scanf() STDIO scanf()
o Assign the octal integer from the next input field to the next
arg, which should be of type int *.
O Assign the octal integer from the next input field to the next
arg, which should be of type long *.
s Assign the string from the next input field to the next arg,
which should be of type char *. The array to which the char *
points should be long enough to accept the string and a ter-
minating null character.
x Assign the hexadecimal integer from the next input field to
the next arg, which should be of type int *.
X Assign the hexadecimal integer from the next input field to
the next arg, which should be of type long *.
It is important to remember that scanf reads up, but not through,
the newline character: the newline remains in the standard input
device's buffer until you dispose of it somehow. Programmers
have been known to forget to empty the buffer before calling
scanf a second time, which leads to unexpected results.
***** Example *****
The following example uses scanf in a brief dialogue with the
user.
#include <stdio.h>
main()
{
int left, right;
printf("No. of fingers on your left hand: ");
/* force message to appear on screen */
fflush(stdout);
scanf("%d", &left);
/* eat newline char */
while(getchar() != '\n')
;
printf("No. of fingers on your right hand: ");
fflush(stdout);
scanf("%d", &right);
COHERENT Lexicon Page 2
scanf() STDIO scanf()
/* again, eat newline */
while(getchar() != '\n')
;
printf("You've %d left fingers, %d right, & %d total\n",
left, right, left+right);
}
***** See Also *****
fscanf(), sscanf(), STDIO
***** Diagnostics *****
scanf returns the number of arguments filled. It returns EOF if
no arguments can be filled or if an error occurs.
***** Notes *****
Because C does not perform type checking, it is essential that an
argument match its specification. For that reason, scanf is best
used to process only data that you are certain are in the correct
data format. The use of upper-case format characters to specify
long arguments is not standard; use the `l' modifier for por-
tability.
scanf is difficult to use correctly, and its misuse can be as-
sociated with intermittent and dangerous bugs. Rather than use
scanf to obtain a string from the keyboard: it is recommended
that you use gets to obtain the string, and use strtok or sscanf
to parse it.
COHERENT Lexicon Page 3