BSD 4_3_Reno release
[unix-history] / usr / src / lib / libc / stdio / scanf.3
CommitLineData
1c15e888 1.\" @(#)scanf.3 6.2 (Berkeley) 4/1/89
3abda555 2.\"
1c15e888 3.TH SCANF 3 "April 1, 1989"
3abda555
KM
4.AT 3
5.SH NAME
6scanf, fscanf, sscanf \- formatted input conversion
7.SH SYNOPSIS
8.B #include <stdio.h>
9.PP
10.B scanf(format
11[ , pointer ] . . .
12.B )
13.br
14.B char *format;
15.PP
16.B fscanf(stream, format
17[ , pointer ] . . .
18.B )
19.br
20.SM
21.B FILE
22.B *stream;
23.br
24.B char *format;
25.PP
26.B sscanf(s, format
27[ , pointer ] . . .
28.B )
29.br
30.B char *s, *format;
31.SH DESCRIPTION
32.I Scanf
33reads from the standard input stream
34e42016 34.BR stdin .
3abda555
KM
35.I Fscanf
36reads from the named input
37.IR stream .
38.I Sscanf
39reads from the character string
40.IR s .
41Each function reads characters, interprets
42them according to a format, and stores the results in its arguments.
43Each expects as arguments
44a control string
34e42016 45.IR format ,
3abda555
KM
46described below,
47and a set of
48.I pointer
49arguments
50indicating where the converted input should be stored.
51.PP
52The
53control string
54usually contains
55conversion specifications, which are used to direct interpretation
56of input sequences.
57The control string may contain:
58.TP 4
591.
60Blanks, tabs or newlines,
61which match optional white space in the input.
62.TP 4
632.
64An ordinary character (not %) which must match
65the next character of the input stream.
66.TP 4
673.
68Conversion specifications, consisting of the
69character
70.BR % ,
71an optional assignment suppressing character
72.BR * ,
73an optional numerical maximum field width, and a conversion
74character.
75.PP
76A conversion specification directs the conversion of the
77next input field; the result
78is placed in the variable pointed to by the corresponding argument,
79unless assignment suppression was
80indicated by
81.BR * .
82An input field is defined as a string of non-space characters;
83it extends to the next inappropriate character or until the field
84width, if specified, is exhausted.
85.PP
86The conversion character indicates the interpretation of the
87input field; the corresponding pointer argument must
88usually be of a restricted type.
89The following conversion characters are legal:
90.TP 4
91.B %
92a single `%' is expected
93in the input at this point;
94no assignment is done.
95.TP 4
96.B d
97a decimal integer is expected;
98the corresponding argument should be an integer pointer.
99.TP 4
100.B o
101an octal integer is expected;
102the corresponding argument should be a integer pointer.
103.TP 4
104.B x
105a hexadecimal integer is expected;
106the corresponding argument should be an integer pointer.
107.ti -0.2i
108.TP 4
109.B s
110a character string is expected;
111the corresponding argument should be a character pointer
112pointing to an array of characters large enough to accept the
113string and a terminating `\e0', which will be added.
114The input field is terminated by a space character
115or a newline.
116.TP 4
117.B c
118a character is expected; the
119corresponding argument should be a character pointer.
120The normal skip over space characters is suppressed
121in this case;
122to read the next non-space character, try
123`%1s'.
124If a field width is given, the corresponding argument
125should refer to a character array, and the
126indicated number of characters is read.
127.TP 4
128\z\fBe\v'1'f\v'-1'\fR
129a
130floating point number is expected;
131the next field is converted accordingly and stored through the
132corresponding argument, which should be a pointer to a
133.IR float .
134The input format for
135floating point numbers is
136an optionally signed
137string of digits
138possibly containing a decimal point, followed by an optional
139exponent field consisting of an E or e followed by an optionally signed integer.
140.TP 4
141.B [
142indicates a string not to be delimited by space characters.
143The left bracket is followed by a set of characters and a right
144bracket; the characters between the brackets define a set
145of characters making up the string.
146If the first character
147is not circumflex (\|^\|), the input field
148is all characters until the first character not in the set between
149the brackets; if the first character
150after the left bracket is ^, the input field is all characters
151until the first character which is in the remaining set of characters
152between the brackets.
153The corresponding argument must point to a character array.
154.PP
155The conversion characters
156.BR d ,
157.B o
158and
159.B x
34e42016 160may be capitalized or preceded by
3abda555
KM
161.B l
162to indicate that a pointer to
163.B long
164rather than to
165.B int
166is in the argument list.
167Similarly, the conversion characters
168.B e
169or
170.B f
171may be capitalized or
172preceded by
173.B l
174to indicate a pointer to
175.B double
176rather than to
177.BR float .
178The conversion characters
179.BR d ,
180.B o
181and
182.B x
34e42016 183may be preceded by
3abda555
KM
184.B h
185to indicate a pointer to
186.B short
187rather than to
188.BR int .
189.PP
190The
191.I scanf
192functions return the number of successfully matched and assigned input
193items.
194This can be used to decide how many input items were found.
195The constant
196.SM
197.B EOF
198is returned upon end of input; note that this is different
199from 0, which means that no conversion was done;
200if conversion was intended, it was frustrated by an
201inappropriate character in the input.
202.PP
203For example, the call
204.IP "" 10
205int i; float x; char name[50];
206.br
207scanf("%d%f%s", &i, &x, name);
208.PP
209with the input line
210.IP
21125 54.32E\(mi1 thompson
212.PP
213will assign to
214.I i
215the value
21625,
217.I x
218the value 5.432, and
219.I name
220will contain
221.IR `thompson\e0' .
222Or,
223.IP
224int i; float x; char name[50];
225.br
226scanf("%2d%f%*d%[1234567890]", &i, &x, name);
227.PP
228with input
229.IP
23056789 0123 56a72
231.PP
232will assign 56 to
34e42016 233.IR i ,
3abda555 234789.0 to
34e42016 235.IR x ,
3abda555
KM
236skip `0123',
237and place the string `56\e0' in
238.IR name .
239The next call to
240.I getchar
241will return `a'.
242.SH "SEE ALSO"
243atof(3),
86198885
KB
244getc(3),
245printf(3)
3abda555
KM
246.SH DIAGNOSTICS
247The
248.I scanf
249functions return
250.SM
251.B EOF
252on end of input,
253and a short count for missing or illegal data items.
254.SH BUGS
255The success of literal matches and suppressed
256assignments is not directly
257determinable.