1*48351Scael.\" 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*48351Scael.\" @(#)scanf.3 6.6 (Berkeley) 04/19/91 946101Sbostic.\" 10*48351Scael.Dd 11*48351Scael.Dt SCANF 3 12*48351Scael.Os 13*48351Scael.Sh NAME 14*48351Scael.Nm scanf , 15*48351Scael.Nm fscanf , 16*48351Scael.Nm sscanf , 17*48351Scael.Nm vfscanf 18*48351Scael.Nd input format conversion 19*48351Scael.Sh SYNOPSIS 20*48351Scael.Fd #include <stdio.h> 21*48351Scael.Ft int 22*48351Scael.Fn scanf "const char *format" ... 23*48351Scael.Ft int 24*48351Scael.Fn fscanf "FILE *stream" "const char *format" ... 25*48351Scael.Fd #include <varargs.h> 26*48351Scael.Ft int 27*48351Scael.Fn sscanf "char *str" "const char *format" ... 28*48351Scael.Ft int 29*48351Scael.Fn vfscanf "FILE *stream" "char *format" "va_list ap" 30*48351Scael.Sh DESCRIPTION 3146101SbosticThe 32*48351Scael.Fn scanf 3346101Sbosticfamily of functions scans input according to a 34*48351Scael.Fa format 3546101Sbosticas described below. 3646101SbosticThis format may contain 37*48351Scael.Em conversion specifiers ; 3846101Sbosticthe results from such conversions, if any, 3946101Sbosticare stored through the 40*48351Scael.Em pointer 4146101Sbosticarguments. 42*48351ScaelThe 43*48351Scael.Fn scanf 44*48351Scaelfunction 45*48351Scaelreads input from the standard input stream 46*48351Scael.Em stdin , 47*48351Scael.Fn fscanf 48*48351Scaelreads input from the stream pointer 49*48351Scael.Fa stream , 5046101Sbosticand 51*48351Scael.Fn sscanf 52*48351Scaelreads its input from the character string pointed to by 53*48351Scael.Fa str . 54*48351ScaelThe 55*48351Scael.Fn vfscanf 56*48351Scaelfunction 5747036Sdonnis analogous to 58*48351Scael.Xr vfprintf 3 59*48351Scaeland reads input from the stream pointer 60*48351Scael.Fa stream 6147036Sdonnusing a variable argument list of pointers (see 62*48351Scael.Xr varargs 3 ) . 6346101SbosticEach successive 64*48351Scael.Em pointer 6546101Sbosticargument must correspond properly with 6646101Sbosticeach successive conversion specifier 6746101Sbostic(but see `suppression' below). 6846101SbosticAll conversions are introduced by the 69*48351Scael.Cm % 7046101Sbostic(percent sign) character. 7120367SmckusickThe 72*48351Scael.Fa format 7346101Sbosticstring 7446101Sbosticmay also contain other characters. 7546101SbosticWhite space (such as blanks, tabs, or newlines) in the 76*48351Scael.Fa format 7746101Sbosticstring match any amount of white space, including none, in the input. 7846101SbosticEverything else 7946101Sbosticmatches only itself. 8046101SbosticScanning stops 8146101Sbosticwhen an input character does not match such a format character. 8246101SbosticScanning also stops 8346101Sbosticwhen an input conversion cannot be made (see below). 84*48351Scael.Sh CONVERSIONS 8546101SbosticFollowing the 86*48351Scael.Cm % 8746101Sbosticcharacter introducing a conversion 8846101Sbosticthere may be a number of 89*48351Scael.Em flag 9046101Sbosticcharacters, as follows: 91*48351Scael.Bl -tag -width indent 92*48351Scael.It Cm * 93*48351ScaelSuppresses assignment. 9446101SbosticThe conversion that follows occurs as usual, but no pointer is used; 9546101Sbosticthe result of the conversion is simply discarded. 96*48351Scael.It Cm h 97*48351ScaelIndicates that the conversion will be one of 98*48351Scael.Cm dioux 9946101Sbosticor 100*48351Scael.Cm n 10146101Sbosticand the next pointer is a pointer to a 102*48351Scael.Em short int 10346101Sbostic(rather than 104*48351Scael.Em int ) . 105*48351Scael.It Cm l 106*48351ScaelIndicates either that the conversion will be one of 107*48351Scael.Cm dioux 10846101Sbosticor 109*48351Scael.Cm n 11046101Sbosticand the next pointer is a pointer to a 111*48351Scael.Em long int 11246101Sbostic(rather than 113*48351Scael.Em int ) , 11446101Sbosticor that the conversion will be one of 115*48351Scael.Cm efg 11646101Sbosticand the next pointer is a pointer to 117*48351Scael.Em double 11846101Sbostic(rather than 119*48351Scael.Em float ) . 120*48351Scael.It Cm L 121*48351ScaelIndicates that the conversion will be 122*48351Scael.Cm efg 12346101Sbosticand the next pointer is a pointer to 124*48351Scael.Em long double . 12546101Sbostic(This type is not implemented; the 126*48351Scael.Cm L 12746101Sbosticflag is currently ignored.) 128*48351Scael.El 129*48351Scael.Pp 13046101SbosticIn addition to these flags, 13146101Sbosticthere may be an optional maximum field width, 13246101Sbosticexpressed as a decimal integer, 13346101Sbosticbetween the 134*48351Scael.Cm % 13546101Sbosticand the conversion. 13646101SbosticIf no width is given, 13746101Sbostica default of `infinity' is used (with one exception, below); 13846101Sbosticotherwise at most this many characters are scanned 13946101Sbosticin processing the conversion. 14046101SbosticBefore conversion begins, 14146101Sbosticmost conversions skip white space; 14246101Sbosticthis white space is not counted against the field width. 143*48351Scael.Pp 14446101SbosticThe following conversions are available: 145*48351Scael.Bl -tag -width XXXX 146*48351Scael.It Cm % 147*48351ScaelMatches a literal `%'. 14846101SbosticThat is, `%%' in the format string 14946101Sbosticmatches a single input `%' character. 150*48351ScaelNo conversion is done, and assignment does not occur. 151*48351Scael.It Cm d 152*48351ScaelMatches an optionally signed decimal integer; 15346101Sbosticthe next pointer must be a pointer to 154*48351Scael.Em int . 155*48351Scael.It Cm D 156*48351ScaelEquivalent to 157*48351Scael.Xr ld ; 15846101Sbosticthis exists only for backwards compatibility. 159*48351Scael.It Cm i 160*48351ScaelMatches an optionally signed integer; 16146101Sbosticthe next pointer must be a pointer to 162*48351Scael.Em int . 163*48351ScaelThe integer is read in base 16 if it begins 164*48351Scaelwith 165*48351Scael.Ql 0x 166*48351Scaelor 167*48351Scael.Ql 0X , 168*48351Scaelin base 8 if it begins with 169*48351Scael.Ql 0 , 170*48351Scaeland in base 10 otherwise. 17146101SbosticOnly characters that correspond to the base are used. 172*48351Scael.It Cm o 173*48351ScaelMatches an octal integer; 17446101Sbosticthe next pointer must be a pointer to 175*48351Scael.Em unsigned int . 176*48351Scael.It Cm O 177*48351ScaelEquivalent to 178*48351Scael.Xr lo ; 17946101Sbosticthis exists for backwards compatibility. 180*48351Scael.It Cm u 181*48351ScaelMatches an optionally signed decimal integer; 18246101Sbosticthe next pointer must be a pointer to 183*48351Scael.Em unsigned int . 184*48351Scael.It Cm x 185*48351ScaelMatches an optionally a signed hexadecimal integer; 18646101Sbosticthe next pointer must be a pointer to 187*48351Scael.Em unsigned int . 188*48351Scael.It Cm X 189*48351ScaelEquivalent to 190*48351Scael.Cm lx ; 191*48351Scaelthis violates the 192*48351Scael.St -ansiC , 19346101Sbosticbut is backwards compatible with previous 194*48351Scael.Ux 19546101Sbosticsystems. 196*48351Scael.It Cm f 197*48351ScaelMatches an optionally signed floating-point number; 19846101Sbosticthe next pointer must be a pointer to 199*48351Scael.Em float . 200*48351Scael.It Cm e 201*48351ScaelEquivalent to 202*48351Scael.Cm f . 203*48351Scael.It Cm g 204*48351ScaelEquivalent to 205*48351Scael.Cm f . 206*48351Scael.It Cm E 207*48351ScaelEquivalent to 208*48351Scael.Cm lf ; 209*48351Scaelthis violates the 210*48351Scael.St -ansiC , 21146101Sbosticbut is backwards compatible with previous 212*48351Scael.Ux 21346101Sbosticsystems. 214*48351Scael.It Cm F 215*48351ScaelEquivalent to 216*48351Scael.Cm lf ; 21746101Sbosticthis exists only for backwards compatibility. 218*48351Scael.It Cm s 219*48351ScaelMatches a sequence of non-white-space characters; 22046101Sbosticthe next pointer must be a pointer to 221*48351Scael.Em char , 222*48351Scaeland the array must be large enough to accept all the sequence and the 223*48351Scaelterminating 224*48351Scael.Dv NUL 225*48351Scaelcharacter. 22646101SbosticThe input string stops at white space 22746101Sbosticor at the maximum field width, whichever occurs first. 228*48351Scael.It Cm c 229*48351ScaelMatches a sequence of 230*48351Scael.Em width 231*48351Scaelcount 232*48351Scaelcharacters (default 1); 23346101Sbosticthe next pointer must be a pointer to 234*48351Scael.Em char , 23546101Sbosticand there must be enough room for all the characters 236*48351Scael(no terminating 237*48351Scael.Dv NUL 238*48351Scaelis added). 23946101SbosticThe usual skip of leading white space is suppressed. 24046101SbosticTo skip white space first, use an explicit space in the format. 241*48351Scael.It Cm \&[ 242*48351ScaelMatches a nonempty sequence of characters from the specified set 243*48351Scaelof accepted characters; 24446101Sbosticthe next pointer must be a pointer to 245*48351Scael.Em char , 24646101Sbosticand there must be enough room for all the characters in the string, 247*48351Scaelplus a terminating 248*48351Scael.Dv NUL 249*48351Scaelcharacter. 25046101SbosticThe usual skip of leading white space is suppressed. 25146101SbosticThe string is to be made up of characters in 25246101Sbostic(or not in) 25346101Sbostica particular set; 25446101Sbosticthe set is defined by the characters between the open bracket 255*48351Scael.Cm [ 25646101Sbosticcharacter 25746101Sbosticand a close bracket 258*48351Scael.Cm ] 25946101Sbosticcharacter. 26046101SbosticThe set 261*48351Scael.Em excludes 26246101Sbosticthose characters 26346101Sbosticif the first character after the open bracket is a circumflex 264*48351Scael.Cm ^ . 26546101SbosticTo include a close bracket in the set, 26646101Sbosticmake it the first character after the open bracket 26746101Sbosticor the circumflex; 26846101Sbosticany other position will end the set. 26946101SbosticThe hyphen character 270*48351Scael.Cm - 27146101Sbosticis also special; 27246101Sbosticwhen placed between two other characters, 27346101Sbosticit adds all intervening characters to the set. 27446101SbosticTo include a hyphen, 27546101Sbosticmake it the last character before the final close bracket. 276*48351ScaelFor instance, 277*48351Scael.Ql [^]0-9-] 27846101Sbosticmeans the set `everything except close bracket, zero through nine, 27946101Sbosticand hyphen'. 280*48351ScaelThe string ends with the appearance of a character not in the 281*48351Scael(or, with a circumflex, in) set 282*48351Scaelor when the field width runs out. 283*48351Scael.It Cm p 284*48351ScaelMatches a pointer value (as printed by 285*48351Scael.Ql %p 286*48351Scaelin 287*48351Scael.Xr printf 3 ) ; 28846101Sbosticthe next pointer must be a pointer to 289*48351Scael.Em void . 290*48351Scael.It Cm n 291*48351ScaelNothing is expected; 29246101Sbosticinstead, the number of characters consumed thus far from the input 29346101Sbosticis stored through the next pointer, 29446101Sbosticwhich must be a pointer to 295*48351Scael.Em int . 29646101SbosticThis is 297*48351Scael.Em not 29846101Sbostica conversion, although it can be suppressed with the 299*48351Scael.Cm * 30046101Sbosticflag. 301*48351Scael.El 302*48351Scael.Pp 30346101SbosticFor backwards compatibility, 304*48351Scaelother conversion characters (except 305*48351Scael.Ql \e0 ) 306*48351Scaelare taken as if they were 307*48351Scael.Ql %d 308*48351Scaelor, if uppercase, 309*48351Scael.Ql %ld , 310*48351Scaeland a `conversion' of 311*48351Scael.Ql %\e0 312*48351Scaelcauses an immediate return of 313*48351Scael.Dv EOF . 31446101SbosticThe 315*48351Scael.Cm F 31620367Smckusickand 317*48351Scael.Cm X 31846101Sbosticconversions will be changed in the future 319*48351Scaelto conform to the 320*48351Scael.Tn ANSI 321*48351ScaelC standard, 32246101Sbosticafter which they will act like 323*48351Scael.Cm f 32420367Smckusickand 325*48351Scael.Cm x 32646101Sbosticrespectively. 327*48351Scael.Pp 328*48351Scael.Sh RETURN VALUES 329*48351ScaelThese 330*48351Scaelfunctions 331*48351Scaelreturn 332*48351Scaelthe number of input items assigned, which can be fewer than provided 333*48351Scaelfor, or even zero, in the event of a matching failure. 334*48351ScaelZero 33546101Sbosticindicates that, while there was input available, 33646101Sbosticno conversions were assigned; 33746101Sbostictypically this is due to an invalid input character, 338*48351Scaelsuch as an alphabetic character for a 339*48351Scael.Ql %d 340*48351Scaelconversion. 341*48351ScaelThe value 342*48351Scael.Dv EOF 343*48351Scaelis returned if an input failure occurs before any conversion such as an 344*48351Scaelend-of-file occurs. If an error or end-of-file occurs after conversion 345*48351Scaelhas begun, 346*48351Scaelthe number of conversions which were successfully completed is returned. 347*48351Scael.Sh SEE ALSO 348*48351Scael.Xr strtol 3 , 349*48351Scael.Xr strtoul 3 , 350*48351Scael.Xr getc 3 , 351*48351Scael.Xr printf 3 352*48351Scael.Sh HISTORY 353*48351ScaelA 354*48351Scael.Nm 355*48351Scaelfunction appeared in 356*48351Scael.At v7 . 357*48351Scael.Sh BUGS 35846101SbosticThe current situation with 359*48351Scael.Cm %F 36046101Sbosticand 361*48351Scael.Cm %X 36246101Sbosticconversions is unfortunate. 363*48351Scael.Pp 36446101SbosticAll of the backwards compatibility formats will be removed in the future. 365*48351Scael.Pp 36647036SdonnThere is no 367*48351Scael.Em vscanf 36847036Sdonnor 369*48351Scael.Em vsscanf . 37047036Sdonn.\" Had to draw the line somewhere! 371