HP-UX Reference (11i v1 05/09) - 3 Library Functions N-Z (vol 7)

s
scanf(3S) scanf(3S)
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:
int i, n1, n2, n3, n4;
n1 = n2 = n3 = n4 = -1;
scanf("%nBEGIN%n %d %nEND%n", &n1, &n2, &i, &n3, &n4);
if (n2 - n1 == 5) puts( "matched BEGIN");
if (n4 - n3 == 3) puts( "matched END");
Here is an example that checks the success of suppressed assignments:
int i, n1, n2;
n1 = n2 = -1;
scanf( "%d %n%*s%n", &i, &n1, &n2);
if (n2 > n1)
printf("successful assignment suppression of %d chars\n", n2-n1);
APPLICATION USAGE
After scanf() or fscanf() is applied to a stream, the stream becomes byte-oriented (see orienta-
tion(5)).
WARNINGS
Trailing white space (including a newline) is left unread unless matched in the control string.
Truncation of multi-byte characters may occur if a field width is used with the conversion character.
AUTHOR
scanf() was developed by AT&T and HP.
SEE ALSO
getc(3S), setlocale(3C), printf(3S), strtod(3C), strtol(3C), orientation(5), thread_safety(5).
HP-UX 11i Version 1: September 2005 4 Hewlett-Packard Company Section 3861