scanf

Formatted input from stdin.

int scanf (char * format [, argument]...);

Required Header
<stdio.h>

Return Value

This function returns the number of fields processed. EOF is returned if the first character attempted to be read resulted in an error or the End Of File.

Parameters

format

  Format control string

argument

  Optional argument pointers to accept the results

Remarks

The scanf function reads stdin based on the format string and converts the input into the data types described by the string placing their values at the locations specified by the optional argument pointers.

The format string specifes which characters are expected in the input, with some special cases as follows: a space specifies that one or more white-space characters are to follow. The percentage sign (%) specifies that an argument pointer will be used to store a converted value from the input. Its format is:

%[*][width][{h | hh | l | ll | L}]type

To specify that a percentage sign should follow in the input, specify "%%"

The optional "*" flag specifies that the next field should be scanned, but no value is to be assigned to an argument pointer location.

The width is an unsigned integer value which specifies the maximum number of characters to process for the next conversion. Fewer than width characters may be processed if the input routine found a character which it could not convert into the current type.

The optional type specifiers are as follows:

h  = half, which results in a short integer being processed
hh = half half, which results in a signed character being processed
l  = long, which results in an long integer or double being processed
ll = long long, which results in a long long integer or double being processed
L  = long double, as that type is not supported in JCC a double is assumed.
The type is the only non-optional character and can be one of the following:

c = the next character is stored at the assumed char * argument location
d = the next integer is stored at the assumed int * argument location
i = the next integer, hexadecimal or octal integer is stored as in d above
o = the next octal integer is stored at the assumed int * argument location
u = the next unsigned integer is stored at the assumed unsigned int * argument location
x = the next hexadecimal value is stored at the assumed int * argument location
e,
E,
f,
g,
G = the next floating point number is stored at the assumed float * argument location
n = no input read,
    the number of characters read so far is stored at the assumed int * argument location
s = the next string is read up until white-space is detected,
    and is stored at the assumed char * argument location
As an alternative to the s type, "[" and "]" may be used to specify the allowable characters to use as an input string. the value "%[a-z]", "%[a-Z]", "%[A-Z]", "%[a-9]", "%[A-9]" or "%[0-9]" may be used to shortcut the range of characters they imply. They may also include other characters at the same time on either side of the range. To specify a "-" character, ensure that it appears last before the closing bracket. To specify a "]" ensure it appears first or after the "^" if the reverse effect is desired:

As a special case, if the first character in the "[" & "]" brackets is a "^" the effects are reversed - to specify a "^" character, ensure that it does not appear as the first character.

"%nc" where n is an unsigned decimal number, stores a string of length n without the null-terminator.

White-space is not skipped before input is read for any type. Specify at least one space in the format control string before any conversion to ensure that white-space is skipped.

Stream I/O Routines

See Also    fscanf, printf, sprintf, sscanf