source: trunk/minix/man/man3/scanf.3@ 11

Last change on this file since 11 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

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