161180Sbostic.\" Copyright (c) 1990, 1991, 1993 261180Sbostic.\" The Regents of the University of California. All rights reserved. 320367Smckusick.\" 446101Sbostic.\" This code is derived from software contributed to Berkeley by 550302Sbostic.\" Chris Torek and the American National Standards Committee X3, 650302Sbostic.\" on Information Processing Systems. 750302Sbostic.\" 846101Sbostic.\" %sccs.include.redist.man% 946101Sbostic.\" 10*65099Smckusick.\" @(#)scanf.3 8.2 (Berkeley) 12/11/93 1146101Sbostic.\" 1248351Scael.Dd 1348351Scael.Dt SCANF 3 1448351Scael.Os 1548351Scael.Sh NAME 1648351Scael.Nm scanf , 1748351Scael.Nm fscanf , 1848351Scael.Nm sscanf , 1948353Sdonn.Nm vscanf , 2048353Sdonn.Nm vsscanf , 2148351Scael.Nm vfscanf 2248351Scael.Nd input format conversion 2348351Scael.Sh SYNOPSIS 2448351Scael.Fd #include <stdio.h> 2548351Scael.Ft int 2648351Scael.Fn scanf "const char *format" ... 2748351Scael.Ft int 2848351Scael.Fn fscanf "FILE *stream" "const char *format" ... 2948353Sdonn.Ft int 3048353Sdonn.Fn sscanf "const char *str" "const char *format" ... 3148724Sdonn.Fd #include <stdarg.h> 3248351Scael.Ft int 3348353Sdonn.Fn vscanf "const char *format" "va_list ap" 3448351Scael.Ft int 3548353Sdonn.Fn vsscanf "const char *str" "const char *format" "va_list ap" 3648353Sdonn.Ft int 3748353Sdonn.Fn vfscanf "FILE *stream" "const char *format" "va_list ap" 3848351Scael.Sh DESCRIPTION 3946101SbosticThe 4048351Scael.Fn scanf 4146101Sbosticfamily of functions scans input according to a 4248351Scael.Fa format 4346101Sbosticas described below. 4446101SbosticThis format may contain 4548351Scael.Em conversion specifiers ; 4646101Sbosticthe results from such conversions, if any, 4746101Sbosticare stored through the 4848351Scael.Em pointer 4946101Sbosticarguments. 5048351ScaelThe 5148351Scael.Fn scanf 5248351Scaelfunction 5348351Scaelreads input from the standard input stream 5448351Scael.Em stdin , 5548351Scael.Fn fscanf 5648351Scaelreads input from the stream pointer 5748351Scael.Fa stream , 5846101Sbosticand 5948351Scael.Fn sscanf 6048351Scaelreads its input from the character string pointed to by 6148351Scael.Fa str . 6248351ScaelThe 6348351Scael.Fn vfscanf 6448351Scaelfunction 6547036Sdonnis analogous to 6648351Scael.Xr vfprintf 3 6748351Scaeland reads input from the stream pointer 6848351Scael.Fa stream 6947036Sdonnusing a variable argument list of pointers (see 7048724Sdonn.Xr stdarg 3 ) . 7148539SdonnThe 7248353Sdonn.Fn vscanf 7348539Sdonnfunction scans a variable argument list from the standard input and 7448539Sdonnthe 7548353Sdonn.Fn vsscanf 7648539Sdonnfunction scans it from a string; 7748353Sdonnthese are analogous to 7848539Sdonnthe 7948353Sdonn.Fn vprintf 8048353Sdonnand 8148353Sdonn.Fn vsprintf 8248539Sdonnfunctions respectively. 8346101SbosticEach successive 8448351Scael.Em pointer 8546101Sbosticargument must correspond properly with 8646101Sbosticeach successive conversion specifier 8746101Sbostic(but see `suppression' below). 8846101SbosticAll conversions are introduced by the 8948351Scael.Cm % 9046101Sbostic(percent sign) character. 9120367SmckusickThe 9248351Scael.Fa format 9346101Sbosticstring 9446101Sbosticmay also contain other characters. 9546101SbosticWhite space (such as blanks, tabs, or newlines) in the 9648351Scael.Fa format 9746101Sbosticstring match any amount of white space, including none, in the input. 9846101SbosticEverything else 9946101Sbosticmatches only itself. 10046101SbosticScanning stops 10146101Sbosticwhen an input character does not match such a format character. 10246101SbosticScanning also stops 10346101Sbosticwhen an input conversion cannot be made (see below). 10448351Scael.Sh CONVERSIONS 10546101SbosticFollowing the 10648351Scael.Cm % 10746101Sbosticcharacter introducing a conversion 10846101Sbosticthere may be a number of 10948351Scael.Em flag 11046101Sbosticcharacters, as follows: 11148351Scael.Bl -tag -width indent 11248351Scael.It Cm * 11348351ScaelSuppresses assignment. 11446101SbosticThe conversion that follows occurs as usual, but no pointer is used; 11546101Sbosticthe result of the conversion is simply discarded. 11648351Scael.It Cm h 11748351ScaelIndicates that the conversion will be one of 11848351Scael.Cm dioux 11946101Sbosticor 12048351Scael.Cm n 12146101Sbosticand the next pointer is a pointer to a 12248351Scael.Em short int 12346101Sbostic(rather than 12448351Scael.Em int ) . 12548351Scael.It Cm l 12648351ScaelIndicates either that the conversion will be one of 12748351Scael.Cm dioux 12846101Sbosticor 12948351Scael.Cm n 13046101Sbosticand the next pointer is a pointer to a 13148351Scael.Em long int 13246101Sbostic(rather than 13348351Scael.Em int ) , 13446101Sbosticor that the conversion will be one of 13548351Scael.Cm efg 13646101Sbosticand the next pointer is a pointer to 13748351Scael.Em double 13846101Sbostic(rather than 13948351Scael.Em float ) . 14048351Scael.It Cm L 14148351ScaelIndicates that the conversion will be 14248351Scael.Cm efg 14346101Sbosticand the next pointer is a pointer to 14448351Scael.Em long double . 14546101Sbostic(This type is not implemented; the 14648351Scael.Cm L 14746101Sbosticflag is currently ignored.) 14848351Scael.El 14948351Scael.Pp 15046101SbosticIn addition to these flags, 15146101Sbosticthere may be an optional maximum field width, 15246101Sbosticexpressed as a decimal integer, 15346101Sbosticbetween the 15448351Scael.Cm % 15546101Sbosticand the conversion. 15646101SbosticIf no width is given, 15746101Sbostica default of `infinity' is used (with one exception, below); 15846101Sbosticotherwise at most this many characters are scanned 15946101Sbosticin processing the conversion. 16046101SbosticBefore conversion begins, 16146101Sbosticmost conversions skip white space; 16246101Sbosticthis white space is not counted against the field width. 16348351Scael.Pp 16446101SbosticThe following conversions are available: 16548351Scael.Bl -tag -width XXXX 16648351Scael.It Cm % 16748351ScaelMatches a literal `%'. 16848371ScaelThat is, `%\&%' in the format string 16946101Sbosticmatches a single input `%' character. 17048351ScaelNo conversion is done, and assignment does not occur. 17148351Scael.It Cm d 17248351ScaelMatches an optionally signed decimal integer; 17346101Sbosticthe next pointer must be a pointer to 17448351Scael.Em int . 17548351Scael.It Cm D 17648351ScaelEquivalent to 17748351Scael.Xr ld ; 17846101Sbosticthis exists only for backwards compatibility. 17948351Scael.It Cm i 18048351ScaelMatches an optionally signed integer; 18146101Sbosticthe next pointer must be a pointer to 18248351Scael.Em int . 18348351ScaelThe integer is read in base 16 if it begins 18448351Scaelwith 18548351Scael.Ql 0x 18648351Scaelor 18748351Scael.Ql 0X , 18848351Scaelin base 8 if it begins with 18948351Scael.Ql 0 , 19048351Scaeland in base 10 otherwise. 19146101SbosticOnly characters that correspond to the base are used. 19248351Scael.It Cm o 19348351ScaelMatches an octal integer; 19446101Sbosticthe next pointer must be a pointer to 19548351Scael.Em unsigned int . 19648351Scael.It Cm O 19748351ScaelEquivalent to 19848351Scael.Xr lo ; 19946101Sbosticthis exists for backwards compatibility. 20048351Scael.It Cm u 20148351ScaelMatches an optionally signed decimal integer; 20246101Sbosticthe next pointer must be a pointer to 20348351Scael.Em unsigned int . 20448351Scael.It Cm x 205*65099SmckusickMatches an optionally signed hexadecimal integer; 20646101Sbosticthe next pointer must be a pointer to 20748351Scael.Em unsigned int . 20848351Scael.It Cm X 20948351ScaelEquivalent to 21048351Scael.Cm lx ; 21148351Scaelthis violates the 21248351Scael.St -ansiC , 21346101Sbosticbut is backwards compatible with previous 21448351Scael.Ux 21546101Sbosticsystems. 21648351Scael.It Cm f 21748351ScaelMatches an optionally signed floating-point number; 21846101Sbosticthe next pointer must be a pointer to 21948351Scael.Em float . 22048351Scael.It Cm e 22148351ScaelEquivalent to 22248351Scael.Cm f . 22348351Scael.It Cm g 22448351ScaelEquivalent to 22548351Scael.Cm f . 22648351Scael.It Cm E 22748351ScaelEquivalent to 22848351Scael.Cm lf ; 22948351Scaelthis violates the 23048351Scael.St -ansiC , 23146101Sbosticbut is backwards compatible with previous 23248351Scael.Ux 23346101Sbosticsystems. 23448351Scael.It Cm F 23548351ScaelEquivalent to 23648351Scael.Cm lf ; 23746101Sbosticthis exists only for backwards compatibility. 23848351Scael.It Cm s 23948351ScaelMatches a sequence of non-white-space characters; 24046101Sbosticthe next pointer must be a pointer to 24148351Scael.Em char , 24248351Scaeland the array must be large enough to accept all the sequence and the 24348351Scaelterminating 24448351Scael.Dv NUL 24548351Scaelcharacter. 24646101SbosticThe input string stops at white space 24746101Sbosticor at the maximum field width, whichever occurs first. 24848351Scael.It Cm c 24948351ScaelMatches a sequence of 25048351Scael.Em width 25148351Scaelcount 25248351Scaelcharacters (default 1); 25346101Sbosticthe next pointer must be a pointer to 25448351Scael.Em char , 25546101Sbosticand there must be enough room for all the characters 25648351Scael(no terminating 25748351Scael.Dv NUL 25848351Scaelis added). 25946101SbosticThe usual skip of leading white space is suppressed. 26046101SbosticTo skip white space first, use an explicit space in the format. 26148351Scael.It Cm \&[ 26248351ScaelMatches a nonempty sequence of characters from the specified set 26348351Scaelof accepted characters; 26446101Sbosticthe next pointer must be a pointer to 26548351Scael.Em char , 26646101Sbosticand there must be enough room for all the characters in the string, 26748351Scaelplus a terminating 26848351Scael.Dv NUL 26948351Scaelcharacter. 27046101SbosticThe usual skip of leading white space is suppressed. 27146101SbosticThe string is to be made up of characters in 27246101Sbostic(or not in) 27346101Sbostica particular set; 27446101Sbosticthe set is defined by the characters between the open bracket 27548351Scael.Cm [ 27646101Sbosticcharacter 27746101Sbosticand a close bracket 27848351Scael.Cm ] 27946101Sbosticcharacter. 28046101SbosticThe set 28148351Scael.Em excludes 28246101Sbosticthose characters 28346101Sbosticif the first character after the open bracket is a circumflex 28448351Scael.Cm ^ . 28546101SbosticTo include a close bracket in the set, 28646101Sbosticmake it the first character after the open bracket 28746101Sbosticor the circumflex; 28846101Sbosticany other position will end the set. 28946101SbosticThe hyphen character 29048351Scael.Cm - 29146101Sbosticis also special; 29246101Sbosticwhen placed between two other characters, 29346101Sbosticit adds all intervening characters to the set. 29446101SbosticTo include a hyphen, 29546101Sbosticmake it the last character before the final close bracket. 29648351ScaelFor instance, 29748351Scael.Ql [^]0-9-] 29846101Sbosticmeans the set `everything except close bracket, zero through nine, 29946101Sbosticand hyphen'. 30048351ScaelThe string ends with the appearance of a character not in the 30148351Scael(or, with a circumflex, in) set 30248351Scaelor when the field width runs out. 30348351Scael.It Cm p 30448351ScaelMatches a pointer value (as printed by 30548351Scael.Ql %p 30648351Scaelin 30748351Scael.Xr printf 3 ) ; 30846101Sbosticthe next pointer must be a pointer to 30948351Scael.Em void . 31048351Scael.It Cm n 31148351ScaelNothing is expected; 31246101Sbosticinstead, the number of characters consumed thus far from the input 31346101Sbosticis stored through the next pointer, 31446101Sbosticwhich must be a pointer to 31548351Scael.Em int . 31646101SbosticThis is 31748351Scael.Em not 31846101Sbostica conversion, although it can be suppressed with the 31948351Scael.Cm * 32046101Sbosticflag. 32148351Scael.El 32248351Scael.Pp 32346101SbosticFor backwards compatibility, 32448351Scaelother conversion characters (except 32548351Scael.Ql \e0 ) 32648351Scaelare taken as if they were 32748351Scael.Ql %d 32848351Scaelor, if uppercase, 32948351Scael.Ql %ld , 33048351Scaeland a `conversion' of 33148351Scael.Ql %\e0 33248351Scaelcauses an immediate return of 33348351Scael.Dv EOF . 33446101SbosticThe 33548351Scael.Cm F 33620367Smckusickand 33748351Scael.Cm X 33846101Sbosticconversions will be changed in the future 33948351Scaelto conform to the 34048351Scael.Tn ANSI 34148351ScaelC standard, 34246101Sbosticafter which they will act like 34348351Scael.Cm f 34420367Smckusickand 34548351Scael.Cm x 34646101Sbosticrespectively. 34748351Scael.Pp 34848351Scael.Sh RETURN VALUES 34948351ScaelThese 35048351Scaelfunctions 35148351Scaelreturn 35248351Scaelthe number of input items assigned, which can be fewer than provided 35348351Scaelfor, or even zero, in the event of a matching failure. 35448351ScaelZero 35546101Sbosticindicates that, while there was input available, 35646101Sbosticno conversions were assigned; 35746101Sbostictypically this is due to an invalid input character, 35848351Scaelsuch as an alphabetic character for a 35948351Scael.Ql %d 36048351Scaelconversion. 36148351ScaelThe value 36248351Scael.Dv EOF 36348351Scaelis returned if an input failure occurs before any conversion such as an 36448351Scaelend-of-file occurs. If an error or end-of-file occurs after conversion 36548351Scaelhas begun, 36648351Scaelthe number of conversions which were successfully completed is returned. 36748351Scael.Sh SEE ALSO 36848351Scael.Xr strtol 3 , 36948351Scael.Xr strtoul 3 , 37057153Sbostic.Xr strtod 3 , 37148351Scael.Xr getc 3 , 37248351Scael.Xr printf 3 37348724Sdonn.Sh STANDARDS 37448724SdonnThe functions 37548724Sdonn.Fn fscanf , 37648724Sdonn.Fn scanf , 37748724Sdonnand 37848724Sdonn.Fn sscanf 37948724Sdonnconform to 38048724Sdonn.St -ansiC . 38148351Scael.Sh HISTORY 38248724SdonnThe functions 38348724Sdonn.Fn vscanf , 38448724Sdonn.Fn vsscanf 38548724Sdonnand 38648724Sdonn.Fn vfscanf 38748724Sdonnare new to this release. 38848351Scael.Sh BUGS 38946101SbosticThe current situation with 39048351Scael.Cm %F 39146101Sbosticand 39248351Scael.Cm %X 39346101Sbosticconversions is unfortunate. 39448351Scael.Pp 39546101SbosticAll of the backwards compatibility formats will be removed in the future. 39657431Sbostic.Pp 39757153SbosticNumerical strings are truncated to 512 characters; for example, 39857153Sbostic.Cm %f 39957153Sbosticand 40057432Sbostic.Cm %d 40157153Sbosticare implicitly 40257153Sbostic.Cm %512f 40357153Sbosticand 40457153Sbostic.Cm %512d . 405