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