xref: /original-bsd/lib/libc/stdio/scanf.3 (revision d272e02a)
Copyright (c) 1990 The Regents of the University of California.
All rights reserved.

This code is derived from software contributed to Berkeley by
Chris Torek.

%sccs.include.redist.man%

@(#)scanf.3 6.4 (Berkeley) 03/06/91

SCANF 3 ""
C 7
NAME
scanf, fscanf, sscanf, vfscanf - formatted input conversion
SYNOPSIS
#include <stdio.h>

scanf(format [ , pointer ... ] )

char *format;

fscanf(stream, format [ , pointer ... ] )

FILE *stream;

char *format;

sscanf(str, format [ , pointer ... ] )

char *str;

char *format;

#include <varargs.h>

vfscanf(stream, format, ap)

FILE *stream;

char *format;

va_list ap;

DESCRIPTION
The scanf family of functions scans input according to a format as described below. This format may contain "conversion specifiers" ; the results from such conversions, if any, are stored through the pointer arguments. Scanf reads the standard input stream stdin , fscanf reads the named input stream , and sscanf reads from the character string str . Vfscanf is analogous to vfprintf and reads from a named input stream using a variable argument list of pointers (see varargs (3)). Each successive pointer argument must correspond properly with each successive conversion specifier (but see `suppression' below). All conversions are introduced by the % (percent sign) character. The format string may also contain other characters. White space (such as blanks, tabs, or newlines) in the format string match any amount of white space, including none, in the input. Everything else matches only itself. Scanning stops when an input character does not match such a format character. Scanning also stops when an input conversion cannot be made (see below).
CONVERSIONS
Following the % character introducing a conversion there may be a number of flag characters, as follows:

4 * suppresses assignment. The conversion that follows occurs as usual, but no pointer is used; the result of the conversion is simply discarded.

4 h indicates that the conversion will be one of dioux or n and the next pointer is a pointer to a short int (rather than int ).

4 l indicates either that the conversion will be one of dioux or n and the next pointer is a pointer to a long int (rather than int ), or that the conversion will be one of efg and the next pointer is a pointer to double (rather than float ).

4 L indicates that the conversion will be efg and the next pointer is a pointer to "long double" . (This type is not implemented; the L flag is currently ignored.)

In addition to these flags, there may be an optional maximum field width, expressed as a decimal integer, between the % and the conversion. If no width is given, a default of `infinity' is used (with one exception, below); otherwise at most this many characters are scanned in processing the conversion. Before conversion begins, most conversions skip white space; this white space is not counted against the field width.

The following conversions are available:

4 % matches a literal `%'. That is, `%%' in the format string matches a single input `%' character. No conversion is done, and no assignment occurs.

4 d a decimal integer is expected; the next pointer must be a pointer to int .

4 D equivalent to ld ; this exists only for backwards compatibility.

4 i an integer is expected; the next pointer must be a pointer to int . The integer is read in base 16 if it begins with `0x' or `0X', in base 8 if it begins with `0', and in base 10 otherwise. Only characters that correspond to the base are used.

4 o an octal integer is expected; the next pointer must be a pointer to "unsigned int" .

4 O equivalent to lo ; this exists for backwards compatibility.

4 u a signed or unsigned decimal integer is expected; the next pointer must be a pointer to "unsigned int" .

4 x a signed or unsigned hexadecimal integer is expected; the next pointer must be a pointer to "unsigned int" .

4 X equivalent to lx ; this violates the ANSI C standard X3.159-1989, but is backwards compatible with previous X systems.

4 f a floating-point number is expected; the next pointer must be a pointer to float .

4 e equivalent to f .

4 g equivalent to f .

4 E equivalent to lf ; this violates the ANSI C standard X3.159-1989, but is backwards compatible with previous X systems.

4 F equivalent to lf ; this exists only for backwards compatibility.

4 s a string is expected; the next pointer must be a pointer to char , and there must be enough room for all the characters in the string, plus a terminating `\e0'. The input string stops at white space or at the maximum field width, whichever occurs first.

4 c width characters (default 1) are expected; the next pointer must be a pointer to char , and there must be enough room for all the characters (no terminating `\e0' is added). The usual skip of leading white space is suppressed. To skip white space first, use an explicit space in the format.

4 [ a string is expected; the next pointer must be a pointer to char , and there must be enough room for all the characters in the string, plus a terminating `\e0'. The usual skip of leading white space is suppressed. The string is to be made up of characters in (or not in) a particular set; the set is defined by the characters between the open bracket [ character and a close bracket ] character. The set excludes those characters if the first character after the open bracket is a circumflex ^ . To include a close bracket in the set, make it the first character after the open bracket or the circumflex; any other position will end the set. The hyphen character - is also special; when placed between two other characters, it adds all intervening characters to the set. To include a hyphen, make it the last character before the final close bracket. For instance, `[^]0-9-]' means the set `everything except close bracket, zero through nine, and hyphen'. The string ends at the first character not in (or, with a circumflex, in) the set, or when the field width runs out.

4 p a pointer value (as printed by `%p' in printf (3)) is expected; the next pointer must be a pointer to void .

4 n nothing is expected; instead, the number of characters consumed thus far from the input is stored through the next pointer, which must be a pointer to int . This is not a conversion, although it can be suppressed with the * flag.

For backwards compatibility, other conversion characters (except '\e0') are taken as if they were `%d' or, if uppercase, `%ld', and a `conversion' of `%\e0' causes an immediate return of EOF . The F and X conversions will be changed in the future to conform to the ANSI C standard, after which they will act like f and x respectively.

The scanf functions return the number of successfully assigned conversions, or EOF if nothing was assigned before the end of input (or an error during input) was encountered. A return value of 0 indicates that, while there was input available, no conversions were assigned; typically this is due to an invalid input character, such as an alphabetic character for a `%d' conversion.

"SEE ALSO"
strtol(3), strtoul(3), getc(3), printf(3)
"RETURN VALUE"
The scanf functions return EOF on end of input, or a short count for missing or illegal data items.
BUGS
The current situation with %F and %X conversions is unfortunate.

All of the backwards compatibility formats will be removed in the future.

There is no vscanf or vsscanf . Had to draw the line somewhere!