xref: /csrg-svn/lib/libc/stdio/scanf.3 (revision 48539)
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*48539Sdonn.\"     @(#)scanf.3	6.9 (Berkeley) 04/23/91
946101Sbostic.\"
1048351Scael.Dd
1148351Scael.Dt SCANF 3
1248351Scael.Os
1348351Scael.Sh NAME
1448351Scael.Nm scanf ,
1548351Scael.Nm fscanf ,
1648351Scael.Nm sscanf ,
1748353Sdonn.Nm vscanf ,
1848353Sdonn.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" ...
2748353Sdonn.Ft int
2848353Sdonn.Fn sscanf "const char *str" "const char *format" ...
2948351Scael.Fd #include <varargs.h>
3048351Scael.Ft int
3148353Sdonn.Fn vscanf "const char *format" "va_list ap"
3248351Scael.Ft int
3348353Sdonn.Fn vsscanf "const char *str" "const char *format" "va_list ap"
3448353Sdonn.Ft int
3548353Sdonn.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*48539SdonnThe
7048353Sdonn.Fn vscanf
71*48539Sdonnfunction scans a variable argument list from the standard input and
72*48539Sdonnthe
7348353Sdonn.Fn vsscanf
74*48539Sdonnfunction scans it from a string;
7548353Sdonnthese are analogous to
76*48539Sdonnthe
7748353Sdonn.Fn vprintf
7848353Sdonnand
7948353Sdonn.Fn vsprintf
80*48539Sdonnfunctions respectively.
8146101SbosticEach successive
8248351Scael.Em pointer
8346101Sbosticargument must correspond properly with
8446101Sbosticeach successive conversion specifier
8546101Sbostic(but see `suppression' below).
8646101SbosticAll conversions are introduced by the
8748351Scael.Cm %
8846101Sbostic(percent sign) character.
8920367SmckusickThe
9048351Scael.Fa format
9146101Sbosticstring
9246101Sbosticmay also contain other characters.
9346101SbosticWhite space (such as blanks, tabs, or newlines) in the
9448351Scael.Fa format
9546101Sbosticstring match any amount of white space, including none, in the input.
9646101SbosticEverything else
9746101Sbosticmatches only itself.
9846101SbosticScanning stops
9946101Sbosticwhen an input character does not match such a format character.
10046101SbosticScanning also stops
10146101Sbosticwhen an input conversion cannot be made (see below).
10248351Scael.Sh CONVERSIONS
10346101SbosticFollowing the
10448351Scael.Cm %
10546101Sbosticcharacter introducing a conversion
10646101Sbosticthere may be a number of
10748351Scael.Em flag
10846101Sbosticcharacters, as follows:
10948351Scael.Bl -tag -width indent
11048351Scael.It Cm *
11148351ScaelSuppresses assignment.
11246101SbosticThe conversion that follows occurs as usual, but no pointer is used;
11346101Sbosticthe result of the conversion is simply discarded.
11448351Scael.It Cm h
11548351ScaelIndicates that the conversion will be one of
11648351Scael.Cm dioux
11746101Sbosticor
11848351Scael.Cm n
11946101Sbosticand the next pointer is a pointer to a
12048351Scael.Em short  int
12146101Sbostic(rather than
12248351Scael.Em int ) .
12348351Scael.It Cm l
12448351ScaelIndicates either that the conversion will be one of
12548351Scael.Cm dioux
12646101Sbosticor
12748351Scael.Cm n
12846101Sbosticand the next pointer is a pointer to a
12948351Scael.Em long  int
13046101Sbostic(rather than
13148351Scael.Em int ) ,
13246101Sbosticor that the conversion will be one of
13348351Scael.Cm efg
13446101Sbosticand the next pointer is a pointer to
13548351Scael.Em double
13646101Sbostic(rather than
13748351Scael.Em float ) .
13848351Scael.It Cm L
13948351ScaelIndicates that the conversion will be
14048351Scael.Cm efg
14146101Sbosticand the next pointer is a pointer to
14248351Scael.Em long double .
14346101Sbostic(This type is not implemented; the
14448351Scael.Cm L
14546101Sbosticflag is currently ignored.)
14648351Scael.El
14748351Scael.Pp
14846101SbosticIn addition to these flags,
14946101Sbosticthere may be an optional maximum field width,
15046101Sbosticexpressed as a decimal integer,
15146101Sbosticbetween the
15248351Scael.Cm %
15346101Sbosticand the conversion.
15446101SbosticIf no width is given,
15546101Sbostica default of `infinity' is used (with one exception, below);
15646101Sbosticotherwise at most this many characters are scanned
15746101Sbosticin processing the conversion.
15846101SbosticBefore conversion begins,
15946101Sbosticmost conversions skip white space;
16046101Sbosticthis white space is not counted against the field width.
16148351Scael.Pp
16246101SbosticThe following conversions are available:
16348351Scael.Bl -tag -width XXXX
16448351Scael.It Cm %
16548351ScaelMatches a literal `%'.
16648371ScaelThat is, `%\&%' in the format string
16746101Sbosticmatches a single input `%' character.
16848351ScaelNo conversion is done, and assignment does not occur.
16948351Scael.It Cm d
17048351ScaelMatches an optionally signed decimal integer;
17146101Sbosticthe next pointer must be a pointer to
17248351Scael.Em int .
17348351Scael.It Cm D
17448351ScaelEquivalent to
17548351Scael.Xr ld ;
17646101Sbosticthis exists only for backwards compatibility.
17748351Scael.It Cm i
17848351ScaelMatches an optionally signed integer;
17946101Sbosticthe next pointer must be a pointer to
18048351Scael.Em int .
18148351ScaelThe integer is read in base 16 if it begins
18248351Scaelwith
18348351Scael.Ql 0x
18448351Scaelor
18548351Scael.Ql 0X ,
18648351Scaelin base 8 if it begins with
18748351Scael.Ql 0 ,
18848351Scaeland in base 10 otherwise.
18946101SbosticOnly characters that correspond to the base are used.
19048351Scael.It Cm o
19148351ScaelMatches an octal integer;
19246101Sbosticthe next pointer must be a pointer to
19348351Scael.Em unsigned int .
19448351Scael.It Cm O
19548351ScaelEquivalent to
19648351Scael.Xr lo ;
19746101Sbosticthis exists for backwards compatibility.
19848351Scael.It Cm u
19948351ScaelMatches an optionally signed decimal integer;
20046101Sbosticthe next pointer must be a pointer to
20148351Scael.Em unsigned int .
20248351Scael.It Cm x
20348351ScaelMatches an optionally a signed hexadecimal integer;
20446101Sbosticthe next pointer must be a pointer to
20548351Scael.Em unsigned int .
20648351Scael.It Cm X
20748351ScaelEquivalent to
20848351Scael.Cm lx ;
20948351Scaelthis violates the
21048351Scael.St -ansiC ,
21146101Sbosticbut is backwards compatible with previous
21248351Scael.Ux
21346101Sbosticsystems.
21448351Scael.It Cm f
21548351ScaelMatches an optionally signed floating-point number;
21646101Sbosticthe next pointer must be a pointer to
21748351Scael.Em float .
21848351Scael.It Cm e
21948351ScaelEquivalent to
22048351Scael.Cm f .
22148351Scael.It Cm g
22248351ScaelEquivalent to
22348351Scael.Cm f .
22448351Scael.It Cm E
22548351ScaelEquivalent to
22648351Scael.Cm lf ;
22748351Scaelthis violates the
22848351Scael.St -ansiC ,
22946101Sbosticbut is backwards compatible with previous
23048351Scael.Ux
23146101Sbosticsystems.
23248351Scael.It Cm F
23348351ScaelEquivalent to
23448351Scael.Cm lf ;
23546101Sbosticthis exists only for backwards compatibility.
23648351Scael.It Cm s
23748351ScaelMatches a sequence of non-white-space characters;
23846101Sbosticthe next pointer must be a pointer to
23948351Scael.Em char ,
24048351Scaeland the array must be large enough to accept all the sequence and the
24148351Scaelterminating
24248351Scael.Dv NUL
24348351Scaelcharacter.
24446101SbosticThe input string stops at white space
24546101Sbosticor at the maximum field width, whichever occurs first.
24648351Scael.It Cm c
24748351ScaelMatches a sequence of
24848351Scael.Em width
24948351Scaelcount
25048351Scaelcharacters (default 1);
25146101Sbosticthe next pointer must be a pointer to
25248351Scael.Em char ,
25346101Sbosticand there must be enough room for all the characters
25448351Scael(no terminating
25548351Scael.Dv NUL
25648351Scaelis added).
25746101SbosticThe usual skip of leading white space is suppressed.
25846101SbosticTo skip white space first, use an explicit space in the format.
25948351Scael.It Cm \&[
26048351ScaelMatches a nonempty sequence of characters from the specified set
26148351Scaelof accepted characters;
26246101Sbosticthe next pointer must be a pointer to
26348351Scael.Em char ,
26446101Sbosticand there must be enough room for all the characters in the string,
26548351Scaelplus a terminating
26648351Scael.Dv NUL
26748351Scaelcharacter.
26846101SbosticThe usual skip of leading white space is suppressed.
26946101SbosticThe string is to be made up of characters in
27046101Sbostic(or not in)
27146101Sbostica particular set;
27246101Sbosticthe set is defined by the characters between the open bracket
27348351Scael.Cm [
27446101Sbosticcharacter
27546101Sbosticand a close bracket
27648351Scael.Cm ]
27746101Sbosticcharacter.
27846101SbosticThe set
27948351Scael.Em excludes
28046101Sbosticthose characters
28146101Sbosticif the first character after the open bracket is a circumflex
28248351Scael.Cm ^ .
28346101SbosticTo include a close bracket in the set,
28446101Sbosticmake it the first character after the open bracket
28546101Sbosticor the circumflex;
28646101Sbosticany other position will end the set.
28746101SbosticThe hyphen character
28848351Scael.Cm -
28946101Sbosticis also special;
29046101Sbosticwhen placed between two other characters,
29146101Sbosticit adds all intervening characters to the set.
29246101SbosticTo include a hyphen,
29346101Sbosticmake it the last character before the final close bracket.
29448351ScaelFor instance,
29548351Scael.Ql [^]0-9-]
29646101Sbosticmeans the set `everything except close bracket, zero through nine,
29746101Sbosticand hyphen'.
29848351ScaelThe string ends with the appearance of a character not in the
29948351Scael(or, with a circumflex, in) set
30048351Scaelor when the field width runs out.
30148351Scael.It Cm p
30248351ScaelMatches a pointer value (as printed by
30348351Scael.Ql %p
30448351Scaelin
30548351Scael.Xr printf 3 ) ;
30646101Sbosticthe next pointer must be a pointer to
30748351Scael.Em void .
30848351Scael.It Cm n
30948351ScaelNothing is expected;
31046101Sbosticinstead, the number of characters consumed thus far from the input
31146101Sbosticis stored through the next pointer,
31246101Sbosticwhich must be a pointer to
31348351Scael.Em int .
31446101SbosticThis is
31548351Scael.Em not
31646101Sbostica conversion, although it can be suppressed with the
31748351Scael.Cm *
31846101Sbosticflag.
31948351Scael.El
32048351Scael.Pp
32146101SbosticFor backwards compatibility,
32248351Scaelother conversion characters (except
32348351Scael.Ql \e0 )
32448351Scaelare taken as if they were
32548351Scael.Ql %d
32648351Scaelor, if uppercase,
32748351Scael.Ql %ld ,
32848351Scaeland a `conversion' of
32948351Scael.Ql %\e0
33048351Scaelcauses an immediate return of
33148351Scael.Dv EOF .
33246101SbosticThe
33348351Scael.Cm F
33420367Smckusickand
33548351Scael.Cm X
33646101Sbosticconversions will be changed in the future
33748351Scaelto conform to the
33848351Scael.Tn ANSI
33948351ScaelC standard,
34046101Sbosticafter which they will act like
34148351Scael.Cm f
34220367Smckusickand
34348351Scael.Cm x
34446101Sbosticrespectively.
34548351Scael.Pp
34648351Scael.Sh RETURN VALUES
34748351ScaelThese
34848351Scaelfunctions
34948351Scaelreturn
35048351Scaelthe number of input items assigned, which can be fewer than provided
35148351Scaelfor, or even zero, in the event of a matching failure.
35248351ScaelZero
35346101Sbosticindicates that, while there was input available,
35446101Sbosticno conversions were assigned;
35546101Sbostictypically this is due to an invalid input character,
35648351Scaelsuch as an alphabetic character for a
35748351Scael.Ql %d
35848351Scaelconversion.
35948351ScaelThe value
36048351Scael.Dv EOF
36148351Scaelis returned if an input failure occurs before any conversion such as an
36248351Scaelend-of-file occurs. If an error or end-of-file occurs after conversion
36348351Scaelhas begun,
36448351Scaelthe number of conversions which were successfully completed is returned.
36548351Scael.Sh SEE ALSO
36648351Scael.Xr strtol 3 ,
36748351Scael.Xr strtoul 3 ,
36848351Scael.Xr getc 3 ,
36948351Scael.Xr printf 3
37048351Scael.Sh HISTORY
37148351ScaelA
37248351Scael.Nm
37348351Scaelfunction appeared in
37448351Scael.At v7 .
37548351Scael.Sh BUGS
37646101SbosticThe current situation with
37748351Scael.Cm %F
37846101Sbosticand
37948351Scael.Cm %X
38046101Sbosticconversions is unfortunate.
38148351Scael.Pp
38246101SbosticAll of the backwards compatibility formats will be removed in the future.
383