xref: /csrg-svn/lib/libc/stdio/scanf.3 (revision 48353)
148351Scael.\" Copyright (c) 1990, 1991 The Regents of the University of California.
246101Sbostic.\" All rights reserved.
320367Smckusick.\"
446101Sbostic.\" This code is derived from software contributed to Berkeley by
546101Sbostic.\" Chris Torek.
646101Sbostic.\" %sccs.include.redist.man%
746101Sbostic.\"
8*48353Sdonn.\"     @(#)scanf.3	6.7 (Berkeley) 04/19/91
946101Sbostic.\"
1048351Scael.Dd
1148351Scael.Dt SCANF 3
1248351Scael.Os
1348351Scael.Sh NAME
1448351Scael.Nm scanf ,
1548351Scael.Nm fscanf ,
1648351Scael.Nm sscanf ,
17*48353Sdonn.Nm vscanf ,
18*48353Sdonn.Nm vsscanf ,
1948351Scael.Nm vfscanf
2048351Scael.Nd input format conversion
2148351Scael.Sh SYNOPSIS
2248351Scael.Fd #include <stdio.h>
2348351Scael.Ft int
2448351Scael.Fn scanf "const char *format" ...
2548351Scael.Ft int
2648351Scael.Fn fscanf "FILE *stream" "const char *format" ...
27*48353Sdonn.Ft int
28*48353Sdonn.Fn sscanf "const char *str" "const char *format" ...
2948351Scael.Fd #include <varargs.h>
3048351Scael.Ft int
31*48353Sdonn.Fn vscanf "const char *format" "va_list ap"
3248351Scael.Ft int
33*48353Sdonn.Fn vsscanf "const char *str" "const char *format" "va_list ap"
34*48353Sdonn.Ft int
35*48353Sdonn.Fn vfscanf "FILE *stream" "const char *format" "va_list ap"
3648351Scael.Sh DESCRIPTION
3746101SbosticThe
3848351Scael.Fn scanf
3946101Sbosticfamily of functions scans input according to a
4048351Scael.Fa format
4146101Sbosticas described below.
4246101SbosticThis format may contain
4348351Scael.Em conversion specifiers ;
4446101Sbosticthe results from such conversions, if any,
4546101Sbosticare stored through the
4648351Scael.Em pointer
4746101Sbosticarguments.
4848351ScaelThe
4948351Scael.Fn scanf
5048351Scaelfunction
5148351Scaelreads input from the standard input stream
5248351Scael.Em stdin ,
5348351Scael.Fn fscanf
5448351Scaelreads input from the stream pointer
5548351Scael.Fa stream ,
5646101Sbosticand
5748351Scael.Fn sscanf
5848351Scaelreads its input from the character string pointed to by
5948351Scael.Fa str .
6048351ScaelThe
6148351Scael.Fn vfscanf
6248351Scaelfunction
6347036Sdonnis analogous to
6448351Scael.Xr vfprintf 3
6548351Scaeland reads input from the stream pointer
6648351Scael.Fa stream
6747036Sdonnusing a variable argument list of pointers (see
6848351Scael.Xr varargs 3 ) .
69*48353Sdonn.Fn vscanf
70*48353Sdonnscans a variable argument list from the standard input and
71*48353Sdonn.Fn vsscanf
72*48353Sdonnscans it from a string;
73*48353Sdonnthese are analogous to
74*48353Sdonn.Fn vprintf
75*48353Sdonnand
76*48353Sdonn.Fn vsprintf
77*48353Sdonnrespectively.
7846101SbosticEach successive
7948351Scael.Em pointer
8046101Sbosticargument must correspond properly with
8146101Sbosticeach successive conversion specifier
8246101Sbostic(but see `suppression' below).
8346101SbosticAll conversions are introduced by the
8448351Scael.Cm %
8546101Sbostic(percent sign) character.
8620367SmckusickThe
8748351Scael.Fa format
8846101Sbosticstring
8946101Sbosticmay also contain other characters.
9046101SbosticWhite space (such as blanks, tabs, or newlines) in the
9148351Scael.Fa format
9246101Sbosticstring match any amount of white space, including none, in the input.
9346101SbosticEverything else
9446101Sbosticmatches only itself.
9546101SbosticScanning stops
9646101Sbosticwhen an input character does not match such a format character.
9746101SbosticScanning also stops
9846101Sbosticwhen an input conversion cannot be made (see below).
9948351Scael.Sh CONVERSIONS
10046101SbosticFollowing the
10148351Scael.Cm %
10246101Sbosticcharacter introducing a conversion
10346101Sbosticthere may be a number of
10448351Scael.Em flag
10546101Sbosticcharacters, as follows:
10648351Scael.Bl -tag -width indent
10748351Scael.It Cm *
10848351ScaelSuppresses assignment.
10946101SbosticThe conversion that follows occurs as usual, but no pointer is used;
11046101Sbosticthe result of the conversion is simply discarded.
11148351Scael.It Cm h
11248351ScaelIndicates that the conversion will be one of
11348351Scael.Cm dioux
11446101Sbosticor
11548351Scael.Cm n
11646101Sbosticand the next pointer is a pointer to a
11748351Scael.Em short  int
11846101Sbostic(rather than
11948351Scael.Em int ) .
12048351Scael.It Cm l
12148351ScaelIndicates either that the conversion will be one of
12248351Scael.Cm dioux
12346101Sbosticor
12448351Scael.Cm n
12546101Sbosticand the next pointer is a pointer to a
12648351Scael.Em long  int
12746101Sbostic(rather than
12848351Scael.Em int ) ,
12946101Sbosticor that the conversion will be one of
13048351Scael.Cm efg
13146101Sbosticand the next pointer is a pointer to
13248351Scael.Em double
13346101Sbostic(rather than
13448351Scael.Em float ) .
13548351Scael.It Cm L
13648351ScaelIndicates that the conversion will be
13748351Scael.Cm efg
13846101Sbosticand the next pointer is a pointer to
13948351Scael.Em long double .
14046101Sbostic(This type is not implemented; the
14148351Scael.Cm L
14246101Sbosticflag is currently ignored.)
14348351Scael.El
14448351Scael.Pp
14546101SbosticIn addition to these flags,
14646101Sbosticthere may be an optional maximum field width,
14746101Sbosticexpressed as a decimal integer,
14846101Sbosticbetween the
14948351Scael.Cm %
15046101Sbosticand the conversion.
15146101SbosticIf no width is given,
15246101Sbostica default of `infinity' is used (with one exception, below);
15346101Sbosticotherwise at most this many characters are scanned
15446101Sbosticin processing the conversion.
15546101SbosticBefore conversion begins,
15646101Sbosticmost conversions skip white space;
15746101Sbosticthis white space is not counted against the field width.
15848351Scael.Pp
15946101SbosticThe following conversions are available:
16048351Scael.Bl -tag -width XXXX
16148351Scael.It Cm %
16248351ScaelMatches a literal `%'.
16346101SbosticThat is, `%%' in the format string
16446101Sbosticmatches a single input `%' character.
16548351ScaelNo conversion is done, and assignment does not occur.
16648351Scael.It Cm d
16748351ScaelMatches an optionally signed decimal integer;
16846101Sbosticthe next pointer must be a pointer to
16948351Scael.Em int .
17048351Scael.It Cm D
17148351ScaelEquivalent to
17248351Scael.Xr ld ;
17346101Sbosticthis exists only for backwards compatibility.
17448351Scael.It Cm i
17548351ScaelMatches an optionally signed integer;
17646101Sbosticthe next pointer must be a pointer to
17748351Scael.Em int .
17848351ScaelThe integer is read in base 16 if it begins
17948351Scaelwith
18048351Scael.Ql 0x
18148351Scaelor
18248351Scael.Ql 0X ,
18348351Scaelin base 8 if it begins with
18448351Scael.Ql 0 ,
18548351Scaeland in base 10 otherwise.
18646101SbosticOnly characters that correspond to the base are used.
18748351Scael.It Cm o
18848351ScaelMatches an octal integer;
18946101Sbosticthe next pointer must be a pointer to
19048351Scael.Em unsigned int .
19148351Scael.It Cm O
19248351ScaelEquivalent to
19348351Scael.Xr lo ;
19446101Sbosticthis exists for backwards compatibility.
19548351Scael.It Cm u
19648351ScaelMatches an optionally signed decimal integer;
19746101Sbosticthe next pointer must be a pointer to
19848351Scael.Em unsigned int .
19948351Scael.It Cm x
20048351ScaelMatches an optionally a signed hexadecimal integer;
20146101Sbosticthe next pointer must be a pointer to
20248351Scael.Em unsigned int .
20348351Scael.It Cm X
20448351ScaelEquivalent to
20548351Scael.Cm lx ;
20648351Scaelthis violates the
20748351Scael.St -ansiC ,
20846101Sbosticbut is backwards compatible with previous
20948351Scael.Ux
21046101Sbosticsystems.
21148351Scael.It Cm f
21248351ScaelMatches an optionally signed floating-point number;
21346101Sbosticthe next pointer must be a pointer to
21448351Scael.Em float .
21548351Scael.It Cm e
21648351ScaelEquivalent to
21748351Scael.Cm f .
21848351Scael.It Cm g
21948351ScaelEquivalent to
22048351Scael.Cm f .
22148351Scael.It Cm E
22248351ScaelEquivalent to
22348351Scael.Cm lf ;
22448351Scaelthis violates the
22548351Scael.St -ansiC ,
22646101Sbosticbut is backwards compatible with previous
22748351Scael.Ux
22846101Sbosticsystems.
22948351Scael.It Cm F
23048351ScaelEquivalent to
23148351Scael.Cm lf ;
23246101Sbosticthis exists only for backwards compatibility.
23348351Scael.It Cm s
23448351ScaelMatches a sequence of non-white-space characters;
23546101Sbosticthe next pointer must be a pointer to
23648351Scael.Em char ,
23748351Scaeland the array must be large enough to accept all the sequence and the
23848351Scaelterminating
23948351Scael.Dv NUL
24048351Scaelcharacter.
24146101SbosticThe input string stops at white space
24246101Sbosticor at the maximum field width, whichever occurs first.
24348351Scael.It Cm c
24448351ScaelMatches a sequence of
24548351Scael.Em width
24648351Scaelcount
24748351Scaelcharacters (default 1);
24846101Sbosticthe next pointer must be a pointer to
24948351Scael.Em char ,
25046101Sbosticand there must be enough room for all the characters
25148351Scael(no terminating
25248351Scael.Dv NUL
25348351Scaelis added).
25446101SbosticThe usual skip of leading white space is suppressed.
25546101SbosticTo skip white space first, use an explicit space in the format.
25648351Scael.It Cm \&[
25748351ScaelMatches a nonempty sequence of characters from the specified set
25848351Scaelof accepted characters;
25946101Sbosticthe next pointer must be a pointer to
26048351Scael.Em char ,
26146101Sbosticand there must be enough room for all the characters in the string,
26248351Scaelplus a terminating
26348351Scael.Dv NUL
26448351Scaelcharacter.
26546101SbosticThe usual skip of leading white space is suppressed.
26646101SbosticThe string is to be made up of characters in
26746101Sbostic(or not in)
26846101Sbostica particular set;
26946101Sbosticthe set is defined by the characters between the open bracket
27048351Scael.Cm [
27146101Sbosticcharacter
27246101Sbosticand a close bracket
27348351Scael.Cm ]
27446101Sbosticcharacter.
27546101SbosticThe set
27648351Scael.Em excludes
27746101Sbosticthose characters
27846101Sbosticif the first character after the open bracket is a circumflex
27948351Scael.Cm ^ .
28046101SbosticTo include a close bracket in the set,
28146101Sbosticmake it the first character after the open bracket
28246101Sbosticor the circumflex;
28346101Sbosticany other position will end the set.
28446101SbosticThe hyphen character
28548351Scael.Cm -
28646101Sbosticis also special;
28746101Sbosticwhen placed between two other characters,
28846101Sbosticit adds all intervening characters to the set.
28946101SbosticTo include a hyphen,
29046101Sbosticmake it the last character before the final close bracket.
29148351ScaelFor instance,
29248351Scael.Ql [^]0-9-]
29346101Sbosticmeans the set `everything except close bracket, zero through nine,
29446101Sbosticand hyphen'.
29548351ScaelThe string ends with the appearance of a character not in the
29648351Scael(or, with a circumflex, in) set
29748351Scaelor when the field width runs out.
29848351Scael.It Cm p
29948351ScaelMatches a pointer value (as printed by
30048351Scael.Ql %p
30148351Scaelin
30248351Scael.Xr printf 3 ) ;
30346101Sbosticthe next pointer must be a pointer to
30448351Scael.Em void .
30548351Scael.It Cm n
30648351ScaelNothing is expected;
30746101Sbosticinstead, the number of characters consumed thus far from the input
30846101Sbosticis stored through the next pointer,
30946101Sbosticwhich must be a pointer to
31048351Scael.Em int .
31146101SbosticThis is
31248351Scael.Em not
31346101Sbostica conversion, although it can be suppressed with the
31448351Scael.Cm *
31546101Sbosticflag.
31648351Scael.El
31748351Scael.Pp
31846101SbosticFor backwards compatibility,
31948351Scaelother conversion characters (except
32048351Scael.Ql \e0 )
32148351Scaelare taken as if they were
32248351Scael.Ql %d
32348351Scaelor, if uppercase,
32448351Scael.Ql %ld ,
32548351Scaeland a `conversion' of
32648351Scael.Ql %\e0
32748351Scaelcauses an immediate return of
32848351Scael.Dv EOF .
32946101SbosticThe
33048351Scael.Cm F
33120367Smckusickand
33248351Scael.Cm X
33346101Sbosticconversions will be changed in the future
33448351Scaelto conform to the
33548351Scael.Tn ANSI
33648351ScaelC standard,
33746101Sbosticafter which they will act like
33848351Scael.Cm f
33920367Smckusickand
34048351Scael.Cm x
34146101Sbosticrespectively.
34248351Scael.Pp
34348351Scael.Sh RETURN VALUES
34448351ScaelThese
34548351Scaelfunctions
34648351Scaelreturn
34748351Scaelthe number of input items assigned, which can be fewer than provided
34848351Scaelfor, or even zero, in the event of a matching failure.
34948351ScaelZero
35046101Sbosticindicates that, while there was input available,
35146101Sbosticno conversions were assigned;
35246101Sbostictypically this is due to an invalid input character,
35348351Scaelsuch as an alphabetic character for a
35448351Scael.Ql %d
35548351Scaelconversion.
35648351ScaelThe value
35748351Scael.Dv EOF
35848351Scaelis returned if an input failure occurs before any conversion such as an
35948351Scaelend-of-file occurs. If an error or end-of-file occurs after conversion
36048351Scaelhas begun,
36148351Scaelthe number of conversions which were successfully completed is returned.
36248351Scael.Sh SEE ALSO
36348351Scael.Xr strtol 3 ,
36448351Scael.Xr strtoul 3 ,
36548351Scael.Xr getc 3 ,
36648351Scael.Xr printf 3
36748351Scael.Sh HISTORY
36848351ScaelA
36948351Scael.Nm
37048351Scaelfunction appeared in
37148351Scael.At v7 .
37248351Scael.Sh BUGS
37346101SbosticThe current situation with
37448351Scael.Cm %F
37546101Sbosticand
37648351Scael.Cm %X
37746101Sbosticconversions is unfortunate.
37848351Scael.Pp
37946101SbosticAll of the backwards compatibility formats will be removed in the future.
38048351Scael.Pp
38147036SdonnThere is no
38248351Scael.Em vscanf
38347036Sdonnor
38448351Scael.Em vsscanf .
38547036Sdonn.\" Had to draw the line somewhere!
386