scanf.3s (2010 09)

s
scanf(3S) scanf(3S)
International Code Set Support
Single- and multi-byte character code sets are supported.
RETURN VALUE
If the input ends before the first conflict or conversion, EOF is returned. Otherwise, these functions
return the number of successfully assigned input items. This number is a short count, or even zero if a
conflict ensues between an input character and the control string.
ERRORS
scanf() and fscanf() fail if data needs to be read into the stream ’s buffer, and:
[EAGAIN] The O_NONBLOCK flag is set for the file descriptor underlying stream and the pro-
cess would be delayed in the read operation.
[EBADF] The file descriptor underlying stream is not a valid file descriptor open for reading.
[EINTR] The read operation was terminated due to the receipt of a signal, and either no data
was transferred or the implementation does not report partial transfer for this file.
[EIO] The process is a member of a background process and is attempting to read from its
controlling terminal, and either the process is ignoring or blocking the
SIGTTIN
signal or the process group of the process is orphaned.
[EILSEQ] The data obtained from the input stream does not form a valid wide character.
Additional
errno values can be set by the underlying read() function (see read (2)).
EXAMPLES
The call:
int i, n; float x; char name[50];
n = scanf("%d%f%s", &i, &x, name);
with the input line:
25 54.32E-1 thompson
assigns to n the value 3,toi the value 25,tox the value
5.432, and name contains thompson\0. Or:
int i; float x; char name[50];
(void) scanf("%2d%f%*d %[0-9]", &i, &x, name);
with input:
56789 0123 56a72
assigns 56 to i , 789.0 to x, skips 0123, and places the string
56\0 in name. The next call to
getchar() (see getc (3S)) returns a.
For another example, to create a language-independent date scanning routine, use:
char month[20]; int day, year;
(void) scanf(format, month, &day, &year);
For American usage, format would point to a string:
%1$s %2$d %3$d
The input:
July 3 1986
would assign July to month , 3 to day and 1986 to year .
For German usage, format would point to a string:
%2$d %1$s %3$d
The input:
3 Juli 1986
would assign
Juli to month, 3 to day and 1986 to year .
The success of literal matches and suppressed assignments can be determined with the
%n conversion
specification. Here is an example that checks the success of literal matches:
4 Hewlett-Packard Company 4 HP-UX 11i Version 3: September 2010