|
|
coherent
scanf() STDIO scanf()
Accept and format input
#include <stdio.h>
iinntt ssccaannff(_f_o_r_m_a_t, _a_r_g_1, ... _a_r_g_N)
cchhaarr *_f_o_r_m_a_t; [ddaattaa ttyyppee] *_a_r_g_1, ... *_a_r_g_N;
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 iinntt.
The following modifiers can be used within the conversion string:
11. The asterisk `*', which indicates that the next input field
should be skipped rather than assigned to the next arg.
22. A string of decimal digits, which specifies a maximum field
width.
33. 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:
cc Assign the next input character to the next arg, which should
be of type cchhaarr *.
dd Assign the decimal integer from the next input field to the
next arg, which should be of type iinntt *.
DD Assign the decimal integer from the next input field to the
next arg, which should be of type lloonngg *.
ee Assign the floating point number from the next input field to
the next arg, which should be of type ffllooaatt *.
EE Assign the floating point number from the next input field to
the next arg, which should be of type ddoouubbllee *.
ff Same as ee.
FF Same as EE.
COHERENT Lexicon Page 1
scanf() STDIO scanf()
oo Assign the octal integer from the next input field to the next
arg, which should be of type iinntt *.
OO Assign the octal integer from the next input field to the next
arg, which should be of type lloonngg *.
ss Assign the string from the next input field to the next arg,
which should be of type cchhaarr *. The array to which the char *
points should be long enough to accept the string and a ter-
minating null character.
xx Assign the hexadecimal integer from the next input field to
the next arg, which should be of type iinntt *.
XX Assign the hexadecimal integer from the next input field to
the next arg, which should be of type lloonngg *.
It is important to remember that ssccaannff 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
ssccaannff a second time, which leads to unexpected results.
***** Example *****
The following example uses ssccaannff 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
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.