
Click above to get retro games delivered to your door ever month!
X-Hacker.org- libc - <b>scanf</b>
[<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
scanf
=====
Syntax
------
#include <stdio.h>
int scanf(const char *format, ...);
Description
-----------
This function scans formatted text from `stdin' and stores it in the
variables pointed to by the arguments. scanf:.
The format string contains regular characters which much match the input
exactly as well as a conversion specifiers, which begin with a percent
symbol. Any whitespace in the format string matches zero or more of any
whitespace characters in the input. Thus, a single space may match a
newline and two tabs in the input. All conversions except `c' and `['
also skip leading whitespace automatically. Each conversion specifier
contains the following fields:
* An asterisk (`*') which indicates that the input should be
converted according to the conversion spec, but not stored
anywhere.
* A width specifier, which specifies the maximum number of input
characters to use in the conversion.
* An optional conversion qualifier, which may be `h' to specify
`short', `l' to specify long ints, or `L' to specify long doubles.
Long long type can be specified by `L' or `ll'.
* The conversion type specifier:
`c'
Copy the next character (or WIDTH characters) to the given
buffer.
`d'
Convert the input to a signed integer.
`e'
`E'
`f'
`g'
`G'
Convert the input to a floating point number.
`i'
Convert the input, determining base automatically by the
presence of `0x' or `0' prefixes. strtol:.
`n'
Store the number of characters scanned so far into the
integer pointed to.
`o'
Convert the input to a signed integer, using base 8.
`p'
Convert the input to a pointer. This is like using the `x'
format.
`s'
Copy the input to the given string, skipping leading
whitespace and copying non-whitespace characters up to the
next whitespace. The string stored is then `NULL'-terminated.
`u'
Convert the input to an unsigned integer.
`x'
`X'
Convert the input to an unsigned integer, using base 16.
`[...]'
Like the `c' format, except only certain characters are
copied. The characters between the brackets determine which
characters are allowed, and thus when the copying stops.
These characters may be regular characters (example:
`[abcd]') or a range of characters (example: `[a-d]'). If
the first character is a caret (`'), then the set specifies
the set of characters that do not get copied (i.e. the set
is negated). To specify that the set contains a
close-bracket (`]'), list that as the first regular character.
`%'
This must match a percent character in the input.
Most conversions make use of `strtol' or `strtoul' to perform the
actual conversions.
Return Value
------------
The number of items successfully matched and assigned. If input ends
before first item is assigned, EOF is returned.
Example
-------
int x, y;
char buf[100];
scanf("%d %d %s", &x, &y, buf);
/* read to end-of-line */
scanf("%d %[\n]\n", &x, buf);
/* read letters only */
scanf("[a-zA-Z]", buf);
See Also: scanf strtol
Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson