]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/scanf.3
wfix
[thirdparty/man-pages.git] / man3 / scanf.3
CommitLineData
fea681da
MK
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
16.\" 3. All advertising materials mentioning features or use of this software
17.\" must display the following acknowledgement:
18.\" This product includes software developed by the University of
19.\" California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\" @(#)scanf.3 6.14 (Berkeley) 1/8/93
37.\"
38.\" Converted for Linux, Mon Nov 29 15:22:01 1993, faith@cs.unc.edu
c13182ef 39.\" modified to resemble the GNU libio setup used in the Linux libc
fea681da
MK
40.\" used in versions 4.x (x>4) and 5 Helmut.Geyer@iwr.uni-heidelberg.de
41.\" Modified, aeb, 970121
c13182ef
MK
42.\" 2005-07-14, mtk, added description of %n$ form; various text
43.\" incorporated from the GNU C library documentation ((C) The
991910a4 44.\" Free Software Foundation); other parts substantially rewritten.
fea681da
MK
45.\"
46.TH SCANF 3 1995-11-01 "LINUX MANPAGE" "Linux Programmer's Manual"
47.SH NAME
48scanf, fscanf, sscanf, vscanf, vsscanf, vfscanf \- input format conversion
49.SH SYNOPSIS
50.nf
51.B #include <stdio.h>
52.na
53.BI "int scanf(const char *" format ", ..." );
54.br
55.BI "int fscanf(FILE *" stream ", const char *" format ", ..." );
56.br
57.BI "int sscanf(const char *" str ", const char *" format ", ..." );
58.sp
59.B #include <stdarg.h>
60.BI "int vscanf(const char *" format ", va_list " ap );
61.br
62.BI "int vsscanf(const char *" str ", const char *" format ", va_list " ap );
63.br
64.BI "int vfscanf(FILE *" stream ", const char *" format ", va_list " ap );
65.ad
66.SH DESCRIPTION
67The
e511ffb6 68.BR scanf ()
991910a4 69family of functions scans input according to
fea681da 70.I format
c13182ef
MK
71as described below.
72This format may contain
991910a4
MK
73.IR "conversion specifications" ;
74the results from such conversions, if any,
75are stored in the locations pointed to by the
c13182ef 76.I pointer
991910a4
MK
77arguments that follow
78.IR format .
c13182ef 79Each
fea681da 80.I pointer
c13182ef 81argument must be of a type that is appropriate for the value returned
991910a4
MK
82by the corresponding conversion specification.
83
84If the number of conversion specifications in
85.I format
86exceeds the number of
87.I pointer
88arguments, the results are undefined.
89If the number of
90.I pointer
91arguments exceeds the number of conversion specifications, then the excess
92.I pointer
93arguments are evaluated, but are otherwise ignored.
94
95The
e511ffb6 96.BR scanf ()
fea681da
MK
97function reads input from the standard input stream
98.IR stdin ,
e511ffb6 99.BR fscanf ()
fea681da
MK
100reads input from the stream pointer
101.IR stream ,
102and
e511ffb6 103.BR sscanf ()
fea681da
MK
104reads its input from the character string pointed to by
105.IR str .
106.PP
107The
e511ffb6 108.BR vfscanf ()
fea681da
MK
109function is analogous to
110.BR vfprintf (3)
111and reads input from the stream pointer
112.I stream
113using a variable argument list of pointers (see
114.BR stdarg (3).
115The
e511ffb6 116.BR vscanf ()
fea681da 117function scans a variable argument list from the standard input and the
e511ffb6 118.BR vsscanf ()
fea681da 119function scans it from a string; these are analogous to the
fb186734 120.BR vprintf (3)
fea681da 121and
fb186734 122.BR vsprintf (3)
fea681da
MK
123functions respectively.
124.PP
991910a4
MK
125The
126.I format
c13182ef
MK
127string consists of a sequence of
128.IR directives
991910a4
MK
129which describe how to process the sequence of input characters.
130If processing of a directive fails, no further input is read, and
e511ffb6 131.BR scanf ()
991910a4
MK
132returns.
133A "failure" can be either of the following:
134.IR "input failure" ,
135meaning that input characters were unavailable, or
136.IR "matching failure" ,
137meaning that the input was inappropriate (see below).
138
139A directive is one of the following:
140.TP
141\(bu
c13182ef 142A sequence of white-space characters (space, tab, newline, etc; see
991910a4
MK
143.BR isspace (3)).
144This directive matches any amount of white space,
145including none, in the input.
146.TP
147\(bu
148An ordinary character (i.e., one other than white space or '%').
149This character must exactly match the next character of input.
150.TP
151\(bu
152A conversion specification, which commences with a '%' (percent) character.
c13182ef 153A sequence of characters from the input is converted according to
991910a4 154this specification, and the result is placed in the corresponding
fea681da 155.I pointer
991910a4
MK
156argument.
157If the next item of input does not match the the conversion specification,
158the conversion fails \(em this is a
159.IR "matching failure" .
160.PP
161Each
c13182ef 162.I conversion specification
991910a4 163in
c13182ef
MK
164.I format
165begins with either the character '%' or the character sequence
991910a4 166"\fB%\fP\fIn\fP\fB$\fP"
faa42a0a 167(see below for the distinction) followed by:
991910a4
MK
168.TP
169\(bu
170An optional '*' assignment-suppression character:
e511ffb6 171.BR scanf ()
991910a4
MK
172reads input as directed by the conversion specification,
173but discards the input.
174No corresponding
175.I pointer
176argument is required, and this specification is not
177included in the count of successful assignments returned by
e511ffb6 178.BR scanf ().
991910a4
MK
179.TP
180\(bu
181An optional 'a' character.
c13182ef 182This is used with string conversions, and relieves the caller of the
991910a4 183need to allocate a corresponding buffer to hold the input: instead,
e511ffb6 184.BR scanf ()
c13182ef 185allocates a buffer of sufficient size,
991910a4
MK
186and assigns the address of this buffer to the corresponding
187.I pointer
188argument, which should be a pointer to a
189.I "char *"
190variable (this variable does not need to be initialised before the call).
191The caller should subsequently
192.BR free (3)
193this buffer when it is no longer required.
194This is a GNU extension;
195C99 employs the 'a' character as a conversion specifier (and
196it can also be used as such in the GNU implementation).
197.TP
198\(bu
199An optional decimal integer which specifies the
200.IR "maximum field width" .
c13182ef
MK
201Reading of characters stops either when this maximum is reached or
202when a non-matching character is found, whichever happens first.
203Most conversions discard initial whitespace characters (the exceptions
204are noted below),
991910a4 205and these discarded characters don't count towards the maximum field width.
c13182ef
MK
206String input conversions store a null terminator ('\\0')
207to mark the end of the input;
991910a4
MK
208the maximum field width does not include this terminator.
209.TP
210\(bu
c13182ef
MK
211An optional
212.IR "type modifier character" .
991910a4
MK
213For example, the
214.B l
c13182ef
MK
215type modifier is used with integer conversions such as
216.I %d
991910a4
MK
217to specify that the corresponding
218.I pointer
c13182ef 219argument refers to a
991910a4 220.I "long int"
c13182ef 221rather than a pointer to an
991910a4
MK
222.IR int .
223.TP
224\(bu
c13182ef 225A
991910a4
MK
226.I "conversion specifier"
227that specifies the type of input conversion to be performed.
c13182ef 228.PP
991910a4 229The conversion specifications in
fea681da 230.I format
c13182ef 231are of two forms, either beginning with '%' or beginning with
991910a4 232"\fB%\fP\fIn\fP\fB$\fP".
c13182ef 233The two forms should not be mixed in the same
fea681da 234.I format
c13182ef 235string, except that a string containing
991910a4
MK
236"\fB%\fP\fIn\fP\fB$\fP"
237specifications can include
238.I %%
c13182ef 239and
991910a4
MK
240.IR %* .
241If
242.I format
243contains '%'
244specifications then these correspond in order with successive
245.I pointer
246arguments.
247In the
248"\fB%\fP\fIn\fP\fB$\fP"
68e1685c 249form (which is specified in POSIX.1-2001, but not C99),
991910a4 250.I n
c13182ef 251is a decimal integer that specifies that the converted input should
991910a4
MK
252be placed in the location referred to by the
253.IR n -th
254.I pointer
255argument following
256.IR format .
fea681da 257.SH CONVERSIONS
991910a4 258The following
c13182ef 259.IR "type modifier characters"
991910a4 260can appear in a conversion specification:
fea681da
MK
261.TP
262.B h
263Indicates that the conversion will be one of
991910a4 264.B diouxX
fea681da
MK
265or
266.B n
267and the next pointer is a pointer to a
991910a4
MK
268.I short int
269or
270.I unsigned short int
fea681da
MK
271(rather than
272.IR int ).
273.TP
991910a4
MK
274.B hh
275As for
276.BR h ,
277but the next pointer is a pointer to a
278.I signed char
279or
280.IR "unsigned char" .
281.TP
282.B j
283As for
284.BR h ,
285but the next pointer is a pointer to a
286.I intmax_t
287or
288.IR uintmax_t .
289This modifier was introduced in C99.
290.TP
fea681da
MK
291.B l
292Indicates either that the conversion will be one of
991910a4 293.B diouxX
fea681da
MK
294or
295.B n
296and the next pointer is a pointer to a
991910a4
MK
297.I long int
298or
299.I unsigned long int
fea681da
MK
300(rather than
301.IR int ),
302or that the conversion will be one of
303.B efg
304and the next pointer is a pointer to
305.I double
306(rather than
307.IR float ).
c13182ef 308Specifying two
fea681da 309.B l
991910a4
MK
310characters is equivalent to
311.BR L .
312If used with
c13182ef
MK
313.I %c
314or
315.I %s
316the corresponding parameter is considered
d0f17b57 317as a pointer to a wide character or wide-character string respectively.
991910a4 318.\" This use of l was introduced in Amendment 1 to ISO C90.
fea681da
MK
319.TP
320.B L
321Indicates that the conversion will be either
322.B efg
323and the next pointer is a pointer to
c13182ef
MK
324.IR "long double"
325or the conversion will be
fea681da
MK
326.B dioux
327and the next pointer is a pointer to
328.IR "long long" .
c13182ef 329.\" MTK, Jul 05: The following is no longer true for modern
991910a4 330.\" ANSI C (i.e., C99):
c13182ef
MK
331.\" (Note that long long is not an
332.\" ANSI C
991910a4
MK
333.\" type. Any program using this will not be portable to all
334.\" architectures).
fea681da
MK
335.TP
336.B q
c13182ef
MK
337equivalent to
338.BR L .
991910a4
MK
339This specifier does not exist in ANSI C.
340.TP
341.B t
342As for
343.BR h ,
344but the next pointer is a pointer to a
345.IR ptrdiff_t .
346This modifier was introduced in C99.
347.TP
348.B z
349As for
350.BR h ,
351but the next pointer is a pointer to a
352.IR size_t .
353This modifier was introduced in C99.
fea681da 354.PP
c13182ef 355The following
991910a4
MK
356.I "conversion specifiers"
357are available:
fea681da
MK
358.TP
359.B %
c13182ef
MK
360Matches a literal '%'.
361That is,
362.I %\&%
991910a4 363in the format string matches a
c13182ef
MK
364single input '%' character.
365No conversion is done, and assignment does not
fea681da
MK
366occur.
367.TP
368.B d
369Matches an optionally signed decimal integer;
370the next pointer must be a pointer to
371.IR int .
372.TP
373.B D
374Equivalent to
991910a4 375.IR ld ;
fea681da 376this exists only for backwards compatibility.
c13182ef
MK
377(Note: thus only in libc4.
378In libc5 and glibc the
379.I %D
991910a4 380is silently ignored, causing old programs to fail mysteriously.)
fea681da
MK
381.TP
382.B i
383Matches an optionally signed integer; the next pointer must be a pointer to
384.IR int .
c13182ef 385The integer is read in base 16 if it begins with
991910a4 386.I 0x
c13182ef
MK
387or
388.IR 0X ,
389in base 8 if it begins with
390.IR 0 ,
991910a4
MK
391and in base 10 otherwise.
392Only characters that correspond to the base are used.
fea681da
MK
393.TP
394.B o
395Matches an unsigned octal integer; the next pointer must be a pointer to
396.IR "unsigned int" .
397.TP
398.B u
399Matches an unsigned decimal integer; the next pointer must be a
400pointer to
401.IR "unsigned int" .
402.TP
403.B x
404Matches an unsigned hexadecimal integer; the next pointer must
405be a pointer to
406.IR "unsigned int" .
407.TP
408.B X
409Equivalent to
410.BR x .
411.TP
412.B f
413Matches an optionally signed floating-point number; the next pointer must
414be a pointer to
415.IR float .
416.TP
417.B e
418Equivalent to
419.BR f .
420.TP
421.B g
422Equivalent to
423.BR f .
424.TP
425.B E
426Equivalent to
427.BR f .
428.TP
991910a4
MK
429.B a
430(C99) Equivalent to
431.BR f .
432.TP
fea681da 433.B s
991910a4 434Matches a sequence of non-white-space characters;
c13182ef
MK
435the next pointer must be a pointer to character array that is
436long enough to hold the input sequence and the terminating null
991910a4
MK
437character ('\\0'), which is added automatically.
438The input string stops at white space or at the maximum field
fea681da
MK
439width, whichever occurs first.
440.TP
441.B c
991910a4
MK
442Matches a sequence of characters whose length is specified by the
443.I maximum field width
444(default 1); the next pointer must be a pointer to
fea681da
MK
445.IR char ,
446and there must be enough room for all the characters (no terminating
28d88c17 447null byte
c13182ef
MK
448is added).
449The usual skip of leading white space is suppressed.
450To skip white space first, use an explicit space in the format.
fea681da
MK
451.TP
452.B \&[
453Matches a nonempty sequence of characters from the specified set of
454accepted characters; the next pointer must be a pointer to
455.IR char ,
456and there must be enough room for all the characters in the string, plus a
28d88c17 457terminating null byte.
c13182ef
MK
458The usual skip of leading white space is suppressed.
459The string is to be made up of characters in (or not in) a particular set;
460the set is defined by the characters between the open bracket
fea681da
MK
461.B [
462character and a close bracket
463.B ]
c13182ef
MK
464character.
465The set
fea681da
MK
466.I excludes
467those characters if the first character after the open bracket is a
468circumflex
43daf87d 469.RB ( ^ ).
fea681da
MK
470To include a close bracket in the set, make it the first character after
471the open bracket or the circumflex; any other position will end the set.
472The hyphen character
8c383102 473.B \-
fea681da 474is also special; when placed between two other characters, it adds all
c13182ef
MK
475intervening characters to the set.
476To include a hyphen, make it the last
477character before the final close bracket.
478For instance,
34131745 479.B [^]0\-9\-]
991910a4
MK
480means
481the set "everything except close bracket, zero through nine, and hyphen".
fea681da
MK
482The string ends with the appearance of a character not in the (or, with a
483circumflex, in) set or when the field width runs out.
484.TP
485.B p
c13182ef 486Matches a pointer value (as printed by
991910a4
MK
487.I %p
488in
fea681da 489.BR printf (3);
475f1bca 490the next pointer must be a pointer to a pointer to
fea681da
MK
491.IR void .
492.TP
493.B n
494Nothing is expected; instead, the number of characters consumed thus far
495from the input is stored through the next pointer, which must be a pointer
496to
497.IR int .
498This is
499.I not
500a conversion, although it can be suppressed with the
501.B *
991910a4 502assignment-suppression character.
c13182ef
MK
503The C standard says: "Execution of a
504.I %n
991910a4
MK
505directive does not increment
506the assignment count returned at the completion of execution"
c13182ef
MK
507but the Corrigendum seems to contradict this.
508Probably it is wise
991910a4 509not to make any assumptions on the effect of
c13182ef 510.I %n
991910a4 511conversions on the return value.
fea681da 512.SH "RETURN VALUE"
c13182ef
MK
513These functions return the number of input items
514successfully matched and assigned,
515which can be fewer than provided for,
991910a4 516or even zero in the event of an early matching failure.
4d9b6984
MK
517
518The value
519.B EOF
c13182ef 520is returned if the end of input is reached before either the first
6e888f06 521successful conversion or a matching failure occurs.
fea681da 522.B EOF
c13182ef 523is also returned if a read error occurs,
4d9b6984
MK
524in which case the error indicator for the stream (see
525.BR ferror (3))
526is set, and
527.I errno
528is set indicate the error.
fea681da
MK
529.SH "SEE ALSO"
530.BR getc (3),
531.BR printf (3),
9fcfcba0 532.BR setlocale (3),
fea681da
MK
533.BR strtod (3),
534.BR strtol (3),
535.BR strtoul (3)
536.SH "CONFORMING TO"
537The functions
e511ffb6
MK
538.BR fscanf (),
539.BR scanf (),
fea681da 540and
e511ffb6 541.BR sscanf ()
68e1685c 542conform to C89 and C99.
fea681da 543.PP
c13182ef 544The
fea681da 545.B q
c13182ef 546specifier is the 4.4BSD notation for
fea681da
MK
547.IR "long long" ,
548while
549.B ll
550or the usage of
551.B L
552in integer conversions is the GNU notation.
553.PP
c13182ef
MK
554The Linux version of these functions is based on the
555.I GNU
fea681da 556.I libio
c13182ef
MK
557library.
558Take a look at the
fea681da 559.I info
c13182ef 560documentation of
fea681da
MK
561.I GNU
562.I libc (glibc-1.08)
c13182ef 563for a more concise description.
fea681da 564.SH BUGS
c13182ef
MK
565All functions are fully C89 conformant, but provide the
566additional specifiers
fea681da
MK
567.B q
568and
569.B a
570as well as an additional behaviour of the
571.B L
c13182ef
MK
572and
573.B l
574specifiers.
575The latter may be considered to be a bug, as it changes the
68e1685c 576behaviour of specifiers defined in C89.
fea681da 577.PP
c13182ef
MK
578Some combinations of the type modifiers and conversion
579specifiers defined by ANSI C do not make sense
580(e.g.
fea681da
MK
581.BR "%Ld" ).
582While they may have a well-defined behaviour on Linux, this need not
c13182ef
MK
583to be so on other architectures.
584Therefore it usually is better to use
585modifiers that are not defined by ANSI C at all, i.e. use
fea681da 586.B q
c13182ef 587instead of
fea681da 588.B L
c13182ef 589in combination with
fea681da 590.B diouxX
c13182ef 591conversions or
fea681da
MK
592.BR ll .
593.PP
594The usage of
595.B q
43daf87d 596is not the same as on 4.4BSD,
c13182ef 597as it may be used in float conversions equivalently to
fea681da 598.BR L .