copyediting for Usenix manuals
[unix-history] / usr / src / lib / libc / stdio / scanf.3
CommitLineData
7860c229
KB
1.\" Copyright (c) 1990, 1991, 1993
2.\" The Regents of the University of California. All rights reserved.
3abda555 3.\"
858d320c 4.\" This code is derived from software contributed to Berkeley by
043368e6
KB
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
858d320c
KB
8.\" %sccs.include.redist.man%
9.\"
653ba8b6 10.\" @(#)scanf.3 8.2 (Berkeley) %G%
858d320c 11.\"
ae59e04c
CL
12.Dd
13.Dt SCANF 3
14.Os
15.Sh NAME
16.Nm scanf ,
17.Nm fscanf ,
18.Nm sscanf ,
905d1132
DS
19.Nm vscanf ,
20.Nm vsscanf ,
ae59e04c
CL
21.Nm vfscanf
22.Nd input format conversion
23.Sh SYNOPSIS
24.Fd #include <stdio.h>
25.Ft int
26.Fn scanf "const char *format" ...
27.Ft int
28.Fn fscanf "FILE *stream" "const char *format" ...
905d1132
DS
29.Ft int
30.Fn sscanf "const char *str" "const char *format" ...
dff3b81f 31.Fd #include <stdarg.h>
ae59e04c 32.Ft int
905d1132
DS
33.Fn vscanf "const char *format" "va_list ap"
34.Ft int
35.Fn vsscanf "const char *str" "const char *format" "va_list ap"
ae59e04c 36.Ft int
905d1132 37.Fn vfscanf "FILE *stream" "const char *format" "va_list ap"
ae59e04c 38.Sh DESCRIPTION
858d320c 39The
ae59e04c 40.Fn scanf
858d320c 41family of functions scans input according to a
ae59e04c 42.Fa format
858d320c
KB
43as described below.
44This format may contain
ae59e04c 45.Em conversion specifiers ;
858d320c
KB
46the results from such conversions, if any,
47are stored through the
ae59e04c 48.Em pointer
858d320c 49arguments.
ae59e04c
CL
50The
51.Fn scanf
52function
53reads input from the standard input stream
54.Em stdin ,
55.Fn fscanf
56reads input from the stream pointer
57.Fa stream ,
858d320c 58and
ae59e04c
CL
59.Fn sscanf
60reads its input from the character string pointed to by
61.Fa str .
62The
63.Fn vfscanf
64function
571f1310 65is analogous to
ae59e04c
CL
66.Xr vfprintf 3
67and reads input from the stream pointer
68.Fa stream
571f1310 69using a variable argument list of pointers (see
dff3b81f 70.Xr stdarg 3 ) .
394cf1c8 71The
905d1132 72.Fn vscanf
394cf1c8
DS
73function scans a variable argument list from the standard input and
74the
905d1132 75.Fn vsscanf
394cf1c8 76function scans it from a string;
905d1132 77these are analogous to
394cf1c8 78the
905d1132
DS
79.Fn vprintf
80and
81.Fn vsprintf
394cf1c8 82functions respectively.
858d320c 83Each successive
ae59e04c 84.Em pointer
858d320c
KB
85argument must correspond properly with
86each successive conversion specifier
87(but see `suppression' below).
88All conversions are introduced by the
ae59e04c 89.Cm %
858d320c 90(percent sign) character.
3abda555 91The
ae59e04c 92.Fa format
858d320c
KB
93string
94may also contain other characters.
95White space (such as blanks, tabs, or newlines) in the
ae59e04c 96.Fa format
858d320c
KB
97string match any amount of white space, including none, in the input.
98Everything else
99matches only itself.
100Scanning stops
101when an input character does not match such a format character.
102Scanning also stops
103when an input conversion cannot be made (see below).
ae59e04c 104.Sh CONVERSIONS
858d320c 105Following the
ae59e04c 106.Cm %
858d320c
KB
107character introducing a conversion
108there may be a number of
ae59e04c 109.Em flag
858d320c 110characters, as follows:
ae59e04c
CL
111.Bl -tag -width indent
112.It Cm *
113Suppresses assignment.
858d320c
KB
114The conversion that follows occurs as usual, but no pointer is used;
115the result of the conversion is simply discarded.
ae59e04c
CL
116.It Cm h
117Indicates that the conversion will be one of
118.Cm dioux
858d320c 119or
ae59e04c 120.Cm n
858d320c 121and the next pointer is a pointer to a
ae59e04c 122.Em short int
858d320c 123(rather than
ae59e04c
CL
124.Em int ) .
125.It Cm l
126Indicates either that the conversion will be one of
127.Cm dioux
858d320c 128or
ae59e04c 129.Cm n
858d320c 130and the next pointer is a pointer to a
ae59e04c 131.Em long int
858d320c 132(rather than
ae59e04c 133.Em int ) ,
858d320c 134or that the conversion will be one of
ae59e04c 135.Cm efg
858d320c 136and the next pointer is a pointer to
ae59e04c 137.Em double
858d320c 138(rather than
ae59e04c
CL
139.Em float ) .
140.It Cm L
141Indicates that the conversion will be
142.Cm efg
858d320c 143and the next pointer is a pointer to
ae59e04c 144.Em long double .
858d320c 145(This type is not implemented; the
ae59e04c 146.Cm L
858d320c 147flag is currently ignored.)
ae59e04c
CL
148.El
149.Pp
858d320c
KB
150In addition to these flags,
151there may be an optional maximum field width,
152expressed as a decimal integer,
153between the
ae59e04c 154.Cm %
858d320c
KB
155and the conversion.
156If no width is given,
157a default of `infinity' is used (with one exception, below);
158otherwise at most this many characters are scanned
159in processing the conversion.
160Before conversion begins,
161most conversions skip white space;
162this white space is not counted against the field width.
ae59e04c 163.Pp
858d320c 164The following conversions are available:
ae59e04c
CL
165.Bl -tag -width XXXX
166.It Cm %
167Matches a literal `%'.
2e313549 168That is, `%\&%' in the format string
858d320c 169matches a single input `%' character.
ae59e04c
CL
170No conversion is done, and assignment does not occur.
171.It Cm d
172Matches an optionally signed decimal integer;
858d320c 173the next pointer must be a pointer to
ae59e04c
CL
174.Em int .
175.It Cm D
176Equivalent to
177.Xr ld ;
858d320c 178this exists only for backwards compatibility.
ae59e04c
CL
179.It Cm i
180Matches an optionally signed integer;
858d320c 181the next pointer must be a pointer to
ae59e04c
CL
182.Em int .
183The integer is read in base 16 if it begins
184with
185.Ql 0x
186or
187.Ql 0X ,
188in base 8 if it begins with
189.Ql 0 ,
190and in base 10 otherwise.
858d320c 191Only characters that correspond to the base are used.
ae59e04c
CL
192.It Cm o
193Matches an octal integer;
858d320c 194the next pointer must be a pointer to
ae59e04c
CL
195.Em unsigned int .
196.It Cm O
197Equivalent to
198.Xr lo ;
858d320c 199this exists for backwards compatibility.
ae59e04c
CL
200.It Cm u
201Matches an optionally signed decimal integer;
858d320c 202the next pointer must be a pointer to
ae59e04c
CL
203.Em unsigned int .
204.It Cm x
653ba8b6 205Matches an optionally signed hexadecimal integer;
858d320c 206the next pointer must be a pointer to
ae59e04c
CL
207.Em unsigned int .
208.It Cm X
209Equivalent to
210.Cm lx ;
211this violates the
212.St -ansiC ,
858d320c 213but is backwards compatible with previous
ae59e04c 214.Ux
858d320c 215systems.
ae59e04c
CL
216.It Cm f
217Matches an optionally signed floating-point number;
858d320c 218the next pointer must be a pointer to
ae59e04c
CL
219.Em float .
220.It Cm e
221Equivalent to
222.Cm f .
223.It Cm g
224Equivalent to
225.Cm f .
226.It Cm E
227Equivalent to
228.Cm lf ;
229this violates the
230.St -ansiC ,
858d320c 231but is backwards compatible with previous
ae59e04c 232.Ux
858d320c 233systems.
ae59e04c
CL
234.It Cm F
235Equivalent to
236.Cm lf ;
858d320c 237this exists only for backwards compatibility.
ae59e04c
CL
238.It Cm s
239Matches a sequence of non-white-space characters;
858d320c 240the next pointer must be a pointer to
ae59e04c
CL
241.Em char ,
242and the array must be large enough to accept all the sequence and the
243terminating
244.Dv NUL
245character.
858d320c
KB
246The input string stops at white space
247or at the maximum field width, whichever occurs first.
ae59e04c
CL
248.It Cm c
249Matches a sequence of
250.Em width
251count
252characters (default 1);
858d320c 253the next pointer must be a pointer to
ae59e04c 254.Em char ,
858d320c 255and there must be enough room for all the characters
ae59e04c
CL
256(no terminating
257.Dv NUL
258is added).
858d320c
KB
259The usual skip of leading white space is suppressed.
260To skip white space first, use an explicit space in the format.
ae59e04c
CL
261.It Cm \&[
262Matches a nonempty sequence of characters from the specified set
263of accepted characters;
858d320c 264the next pointer must be a pointer to
ae59e04c 265.Em char ,
858d320c 266and there must be enough room for all the characters in the string,
ae59e04c
CL
267plus a terminating
268.Dv NUL
269character.
858d320c
KB
270The usual skip of leading white space is suppressed.
271The string is to be made up of characters in
272(or not in)
273a particular set;
274the set is defined by the characters between the open bracket
ae59e04c 275.Cm [
858d320c
KB
276character
277and a close bracket
ae59e04c 278.Cm ]
858d320c
KB
279character.
280The set
ae59e04c 281.Em excludes
858d320c
KB
282those characters
283if the first character after the open bracket is a circumflex
ae59e04c 284.Cm ^ .
858d320c
KB
285To include a close bracket in the set,
286make it the first character after the open bracket
287or the circumflex;
288any other position will end the set.
289The hyphen character
ae59e04c 290.Cm -
858d320c
KB
291is also special;
292when placed between two other characters,
293it adds all intervening characters to the set.
294To include a hyphen,
295make it the last character before the final close bracket.
ae59e04c
CL
296For instance,
297.Ql [^]0-9-]
858d320c
KB
298means the set `everything except close bracket, zero through nine,
299and hyphen'.
ae59e04c
CL
300The string ends with the appearance of a character not in the
301(or, with a circumflex, in) set
302or when the field width runs out.
303.It Cm p
304Matches a pointer value (as printed by
305.Ql %p
306in
307.Xr printf 3 ) ;
858d320c 308the next pointer must be a pointer to
ae59e04c
CL
309.Em void .
310.It Cm n
311Nothing is expected;
858d320c
KB
312instead, the number of characters consumed thus far from the input
313is stored through the next pointer,
314which must be a pointer to
ae59e04c 315.Em int .
858d320c 316This is
ae59e04c 317.Em not
858d320c 318a conversion, although it can be suppressed with the
ae59e04c 319.Cm *
858d320c 320flag.
ae59e04c
CL
321.El
322.Pp
858d320c 323For backwards compatibility,
ae59e04c
CL
324other conversion characters (except
325.Ql \e0 )
326are taken as if they were
327.Ql %d
328or, if uppercase,
329.Ql %ld ,
330and a `conversion' of
331.Ql %\e0
332causes an immediate return of
333.Dv EOF .
858d320c 334The
ae59e04c 335.Cm F
3abda555 336and
ae59e04c 337.Cm X
858d320c 338conversions will be changed in the future
ae59e04c
CL
339to conform to the
340.Tn ANSI
341C standard,
858d320c 342after which they will act like
ae59e04c 343.Cm f
3abda555 344and
ae59e04c 345.Cm x
858d320c 346respectively.
ae59e04c
CL
347.Pp
348.Sh RETURN VALUES
349These
350functions
351return
352the number of input items assigned, which can be fewer than provided
353for, or even zero, in the event of a matching failure.
354Zero
858d320c
KB
355indicates that, while there was input available,
356no conversions were assigned;
357typically this is due to an invalid input character,
ae59e04c
CL
358such as an alphabetic character for a
359.Ql %d
360conversion.
361The value
362.Dv EOF
363is returned if an input failure occurs before any conversion such as an
364end-of-file occurs. If an error or end-of-file occurs after conversion
365has begun,
366the number of conversions which were successfully completed is returned.
367.Sh SEE ALSO
368.Xr strtol 3 ,
369.Xr strtoul 3 ,
6830cff0 370.Xr strtod 3 ,
ae59e04c
CL
371.Xr getc 3 ,
372.Xr printf 3
dff3b81f
DS
373.Sh STANDARDS
374The functions
375.Fn fscanf ,
376.Fn scanf ,
377and
378.Fn sscanf
379conform to
380.St -ansiC .
ae59e04c 381.Sh HISTORY
dff3b81f
DS
382The functions
383.Fn vscanf ,
384.Fn vsscanf
385and
386.Fn vfscanf
387are new to this release.
ae59e04c 388.Sh BUGS
858d320c 389The current situation with
ae59e04c 390.Cm %F
858d320c 391and
ae59e04c 392.Cm %X
858d320c 393conversions is unfortunate.
ae59e04c 394.Pp
858d320c 395All of the backwards compatibility formats will be removed in the future.
80d40f87 396.Pp
6830cff0
KB
397Numerical strings are truncated to 512 characters; for example,
398.Cm %f
399and
80d40f87 400.Cm %d
6830cff0
KB
401are implicitly
402.Cm %512f
403and
404.Cm %512d .