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