1.\" Copyright (c) 1990, 1991, 1993 2.\" The Regents of the University of California. 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 8.2 (Berkeley) 12/11/93 37.\" $FreeBSD: src/lib/libc/stdio/scanf.3,v 1.7.2.6 2002/04/12 16:34:38 trhodes Exp $ 38.\" $DragonFly: src/lib/libc/stdio/scanf.3,v 1.2 2003/06/17 04:26:46 dillon Exp $ 39.\" 40.Dd December 11, 1993 41.Dt SCANF 3 42.Os 43.Sh NAME 44.Nm scanf , 45.Nm fscanf , 46.Nm sscanf , 47.Nm vscanf , 48.Nm vsscanf , 49.Nm vfscanf 50.Nd input format conversion 51.Sh LIBRARY 52.Lb libc 53.Sh SYNOPSIS 54.In stdio.h 55.Ft int 56.Fn scanf "const char *format" ... 57.Ft int 58.Fn fscanf "FILE *stream" "const char *format" ... 59.Ft int 60.Fn sscanf "const char *str" "const char *format" ... 61.In stdarg.h 62.Ft int 63.Fn vscanf "const char *format" "va_list ap" 64.Ft int 65.Fn vsscanf "const char *str" "const char *format" "va_list ap" 66.Ft int 67.Fn vfscanf "FILE *stream" "const char *format" "va_list ap" 68.Sh DESCRIPTION 69The 70.Fn scanf 71family of functions scans input according to a 72.Fa format 73as described below. 74This format may contain 75.Em conversion specifiers ; 76the results from such conversions, if any, 77are stored through the 78.Em pointer 79arguments. 80The 81.Fn scanf 82function 83reads input from the standard input stream 84.Em stdin , 85.Fn fscanf 86reads input from the stream pointer 87.Fa stream , 88and 89.Fn sscanf 90reads its input from the character string pointed to by 91.Fa str . 92The 93.Fn vfscanf 94function 95is analogous to 96.Xr vfprintf 3 97and reads input from the stream pointer 98.Fa stream 99using a variable argument list of pointers (see 100.Xr stdarg 3 ) . 101The 102.Fn vscanf 103function scans a variable argument list from the standard input and 104the 105.Fn vsscanf 106function scans it from a string; 107these are analogous to 108the 109.Fn vprintf 110and 111.Fn vsprintf 112functions respectively. 113Each successive 114.Em pointer 115argument must correspond properly with 116each successive conversion specifier 117(but see the 118.Cm * 119conversion below). 120All conversions are introduced by the 121.Cm % 122(percent sign) character. 123The 124.Fa format 125string 126may also contain other characters. 127White space (such as blanks, tabs, or newlines) in the 128.Fa format 129string match any amount of white space, including none, in the input. 130Everything else 131matches only itself. 132Scanning stops 133when an input character does not match such a format character. 134Scanning also stops 135when an input conversion cannot be made (see below). 136.Sh CONVERSIONS 137Following the 138.Cm % 139character introducing a conversion 140there may be a number of 141.Em flag 142characters, as follows: 143.Bl -tag -width indent 144.It Cm * 145Suppresses assignment. 146The conversion that follows occurs as usual, but no pointer is used; 147the result of the conversion is simply discarded. 148.It Cm h 149Indicates that the conversion will be one of 150.Cm dioux 151or 152.Cm n 153and the next pointer is a pointer to a 154.Em short int 155(rather than 156.Em int ) . 157.It Cm l 158Indicates either that the conversion will be one of 159.Cm dioux 160or 161.Cm n 162and the next pointer is a pointer to a 163.Em long int 164(rather than 165.Em int ) , 166or that the conversion will be one of 167.Cm efg 168and the next pointer is a pointer to 169.Em double 170(rather than 171.Em float ) . 172.It Cm L 173Indicates that the conversion will be 174.Cm efg 175and the next pointer is a pointer to 176.Em long double . 177(This type is not implemented; the 178.Cm L 179flag is currently ignored.) 180.It Cm q 181Indicates either that the conversion will be one of 182.Cm dioux 183or 184.Cm n 185and the next pointer is a pointer to a 186.Em long long int 187(rather than 188.Em int ) , 189.El 190.Pp 191In addition to these flags, 192there may be an optional maximum field width, 193expressed as a decimal integer, 194between the 195.Cm % 196and the conversion. 197If no width is given, 198a default of `infinity' is used (with one exception, below); 199otherwise at most this many characters are scanned 200in processing the conversion. 201Before conversion begins, 202most conversions skip white space; 203this white space is not counted against the field width. 204.Pp 205The following conversions are available: 206.Bl -tag -width XXXX 207.It Cm % 208Matches a literal `%'. 209That is, `%\&%' in the format string 210matches a single input `%' character. 211No conversion is done, and assignment does not occur. 212.It Cm d 213Matches an optionally signed decimal integer; 214the next pointer must be a pointer to 215.Em int . 216.It Cm D 217Equivalent to 218.Cm ld ; 219this exists only for backwards compatibility. 220.It Cm i 221Matches an optionally signed integer; 222the next pointer must be a pointer to 223.Em int . 224The integer is read in base 16 if it begins 225with 226.Ql 0x 227or 228.Ql 0X , 229in base 8 if it begins with 230.Ql 0 , 231and in base 10 otherwise. 232Only characters that correspond to the base are used. 233.It Cm o 234Matches an octal integer; 235the next pointer must be a pointer to 236.Em unsigned int . 237.It Cm O 238Equivalent to 239.Cm lo ; 240this exists for backwards compatibility. 241.It Cm u 242Matches an optionally signed decimal integer; 243the next pointer must be a pointer to 244.Em unsigned int . 245.It Cm x 246Matches an optionally signed hexadecimal integer; 247the next pointer must be a pointer to 248.Em unsigned int . 249.It Cm X 250Equivalent to 251.Cm lx ; 252this violates the 253.St -isoC , 254but is backwards compatible with previous 255.Ux 256systems. 257.It Cm f 258Matches an optionally signed floating-point number; 259the next pointer must be a pointer to 260.Em float . 261.It Cm e 262Equivalent to 263.Cm f . 264.It Cm g 265Equivalent to 266.Cm f . 267.It Cm E 268Equivalent to 269.Cm lf ; 270this violates the 271.St -isoC , 272but is backwards compatible with previous 273.Ux 274systems. 275.It Cm F 276Equivalent to 277.Cm lf ; 278this exists only for backwards compatibility. 279.It Cm s 280Matches a sequence of non-white-space characters; 281the next pointer must be a pointer to 282.Em char , 283and the array must be large enough to accept all the sequence and the 284terminating 285.Dv NUL 286character. 287The input string stops at white space 288or at the maximum field width, whichever occurs first. 289.It Cm c 290Matches a sequence of 291.Em width 292count 293characters (default 1); 294the next pointer must be a pointer to 295.Em char , 296and there must be enough room for all the characters 297(no terminating 298.Dv NUL 299is added). 300The usual skip of leading white space is suppressed. 301To skip white space first, use an explicit space in the format. 302.It Cm \&[ 303Matches a nonempty sequence of characters from the specified set 304of accepted characters; 305the next pointer must be a pointer to 306.Em char , 307and there must be enough room for all the characters in the string, 308plus a terminating 309.Dv NUL 310character. 311The usual skip of leading white space is suppressed. 312The string is to be made up of characters in 313(or not in) 314a particular set; 315the set is defined by the characters between the open bracket 316.Cm [ 317character 318and a close bracket 319.Cm ] 320character. 321The set 322.Em excludes 323those characters 324if the first character after the open bracket is a circumflex 325.Cm ^ . 326To include a close bracket in the set, 327make it the first character after the open bracket 328or the circumflex; 329any other position will end the set. 330The hyphen character 331.Cm - 332is also special; 333when placed between two other characters, 334it adds all intervening characters to the set. 335To include a hyphen, 336make it the last character before the final close bracket. 337For instance, 338.Ql [^]0-9-] 339means the set `everything except close bracket, zero through nine, 340and hyphen'. 341The string ends with the appearance of a character not in the 342(or, with a circumflex, in) set 343or when the field width runs out. 344.It Cm p 345Matches a pointer value (as printed by 346.Ql %p 347in 348.Xr printf 3 ) ; 349the next pointer must be a pointer to 350.Em void . 351.It Cm n 352Nothing is expected; 353instead, the number of characters consumed thus far from the input 354is stored through the next pointer, 355which must be a pointer to 356.Em int . 357This is 358.Em not 359a conversion, although it can be suppressed with the 360.Cm * 361flag. 362.El 363.Pp 364For backwards compatibility, 365other conversion characters (except 366.Ql \e0 ) 367are taken as if they were 368.Ql %d 369or, if uppercase, 370.Ql %ld , 371and a `conversion' of 372.Ql %\e0 373causes an immediate return of 374.Dv EOF . 375The 376.Cm F 377and 378.Cm X 379conversions will be changed in the future 380to conform to the 381.Tn ANSI 382C standard, 383after which they will act like 384.Cm f 385and 386.Cm x 387respectively. 388.Sh RETURN VALUES 389These 390functions 391return 392the number of input items assigned, which can be fewer than provided 393for, or even zero, in the event of a matching failure. 394Zero 395indicates that, while there was input available, 396no conversions were assigned; 397typically this is due to an invalid input character, 398such as an alphabetic character for a 399.Ql %d 400conversion. 401The value 402.Dv EOF 403is returned if an input failure occurs before any conversion such as an 404end-of-file occurs. 405If an error or end-of-file occurs after conversion 406has begun, 407the number of conversions which were successfully completed is returned. 408.Sh SEE ALSO 409.Xr getc 3 , 410.Xr printf 3 , 411.Xr strtod 3 , 412.Xr strtol 3 , 413.Xr strtoul 3 414.Sh STANDARDS 415The functions 416.Fn fscanf , 417.Fn scanf , 418and 419.Fn sscanf 420conform to 421.St -isoC . 422.Sh BUGS 423The current situation with 424.Cm %F 425and 426.Cm %X 427conversions is unfortunate. 428.Pp 429All of the backwards compatibility formats will be removed in the future. 430.Pp 431Numerical strings are truncated to 512 characters; for example, 432.Cm %f 433and 434.Cm %d 435are implicitly 436.Cm %512f 437and 438.Cm %512d . 439