1.\" $NetBSD: scanf.3,v 1.19 2003/09/08 17:54:32 wiz Exp $ 2.\" 3.\" Copyright (c) 1990, 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" This code is derived from software contributed to Berkeley by 7.\" Chris Torek and the American National Standards Committee X3, 8.\" on Information Processing Systems. 9.\" 10.\" Redistribution and use in source and binary forms, with or without 11.\" modification, are permitted provided that the following conditions 12.\" are met: 13.\" 1. Redistributions of source code must retain the above copyright 14.\" notice, this list of conditions and the following disclaimer. 15.\" 2. Redistributions in binary form must reproduce the above copyright 16.\" notice, this list of conditions and the following disclaimer in the 17.\" documentation and/or other materials provided with the distribution. 18.\" 3. Neither the name of the University nor the names of its contributors 19.\" may be used to endorse or promote products derived from this software 20.\" without specific prior written permission. 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" SUCH DAMAGE. 33.\" 34.\" @(#)scanf.3 8.2 (Berkeley) 12/11/93 35.\" 36.Dd April 30, 2001 37.Dt SCANF 3 38.Os 39.Sh NAME 40.Nm scanf , 41.Nm fscanf , 42.Nm sscanf , 43.Nm vscanf , 44.Nm vsscanf , 45.Nm vfscanf 46.Nd input format conversion 47.Sh LIBRARY 48.Lb libc 49.Sh SYNOPSIS 50.In stdio.h 51.Ft int 52.Fn scanf "const char * restrict format" ... 53.Ft int 54.Fn fscanf "FILE * restrict stream" "const char * restrict format" ... 55.Ft int 56.Fn sscanf "const char * restrict str" "const char * restrict format" ... 57.In stdarg.h 58.Ft int 59.Fn vscanf "const char * restrict format" "va_list ap" 60.Ft int 61.Fn vsscanf "const char * restrict str" "const char * restrict format" "va_list ap" 62.Ft int 63.Fn vfscanf "FILE * restrict stream" "const char * restrict format" "va_list ap" 64.Sh DESCRIPTION 65The 66.Fn scanf 67family of functions scans input according to a 68.Fa format 69as described below. 70This format may contain 71.Em conversion specifiers ; 72the results from such conversions, if any, 73are stored through the 74.Em pointer 75arguments. 76The 77.Fn scanf 78function 79reads input from the standard input stream 80.Em stdin , 81.Fn fscanf 82reads input from the stream pointer 83.Fa stream , 84and 85.Fn sscanf 86reads its input from the character string pointed to by 87.Fa str . 88The 89.Fn vfscanf 90function 91is analogous to 92.Xr vfprintf 3 93and reads input from the stream pointer 94.Fa stream 95using a variable argument list of pointers (see 96.Xr stdarg 3 ) . 97The 98.Fn vscanf 99function scans a variable argument list from the standard input and 100the 101.Fn vsscanf 102function scans it from a string; 103these are analogous to 104the 105.Fn vprintf 106and 107.Fn vsprintf 108functions respectively. 109Each successive 110.Em pointer 111argument must correspond properly with 112each successive conversion specifier 113(but see `suppression' below). 114All conversions are introduced by the 115.Cm % 116(percent sign) character. 117The 118.Fa format 119string 120may also contain other characters. 121White space (such as blanks, tabs, or newlines) in the 122.Fa format 123string match any amount of white space, including none, in the input. 124Everything else 125matches only itself. 126Scanning stops 127when an input character does not match such a format character. 128Scanning also stops 129when an input conversion cannot be made (see below). 130.Sh CONVERSIONS 131Following the 132.Cm % 133character introducing a conversion 134there may be a number of 135.Em flag 136characters, as follows: 137.Bl -tag -width indent 138.It Cm * 139Suppresses assignment. 140The conversion that follows occurs as usual, but no pointer is used; 141the result of the conversion is simply discarded. 142.It Cm h 143Indicates that the conversion will be one of 144.Cm dioux 145or 146.Cm n 147and the next pointer is a pointer to a 148.Em short int 149(rather than 150.Em int ) . 151.It Cm j 152Indicates that the conversion will be one of 153.Cm dioux 154or 155.Cm n 156and the next pointer is a pointer to an 157.Em intmax_t 158(rather than 159.Em int ) . 160.It Cm l 161Indicates either that the conversion will be one of 162.Cm dioux 163or 164.Cm n 165and the next pointer is a pointer to a 166.Em long int 167(rather than 168.Em int ) , 169or that the conversion will be one of 170.Cm efg 171and the next pointer is a pointer to 172.Em double 173(rather than 174.Em float ) . 175.It Cm q 176Indicates that the conversion will be one of 177.Cm dioux 178or 179.Cm n 180and the next pointer is a pointer to a 181.Em quad_t 182(rather than 183.Em int ) . 184.It Cm t 185Indicates that the conversion will be one of 186.Cm dioux 187or 188.Cm n 189and the next pointer is a pointer to a 190.Em ptrdiff_t 191(rather than 192.Em int ) . 193.It Cm z 194Indicates that the conversion will be one of 195.Cm dioux 196or 197.Cm n 198and the next pointer is a pointer to a 199.Em size_t 200(rather than 201.Em int ) . 202.It Cm L 203Indicates that the conversion will be 204.Cm efg 205and the next pointer is a pointer to 206.Em long double . 207.El 208.Pp 209In addition to these flags, 210there may be an optional maximum field width, 211expressed as a decimal integer, 212between the 213.Cm % 214and the conversion. 215If no width is given, 216a default of `infinity' is used (with one exception, below); 217otherwise at most this many characters are scanned 218in processing the conversion. 219Before conversion begins, 220most conversions skip white space; 221this white space is not counted against the field width. 222.Pp 223The following conversions are available: 224.Bl -tag -width XXXX 225.It Cm % 226Matches a literal `%'. 227That is, `%\&%' in the format string 228matches a single input `%' character. 229No conversion is done, and assignment does not occur. 230.It Cm d 231Matches an optionally signed decimal integer; 232the next pointer must be a pointer to 233.Em int . 234.It Cm D 235Equivalent to 236.Cm ld ; 237this exists only for backwards compatibility. 238.It Cm i 239Matches an optionally signed integer; 240the next pointer must be a pointer to 241.Em int . 242The integer is read in base 16 if it begins 243with 244.Ql 0x 245or 246.Ql 0X , 247in base 8 if it begins with 248.Ql 0 , 249and in base 10 otherwise. 250Only characters that correspond to the base are used. 251.It Cm o 252Matches an octal integer; 253the next pointer must be a pointer to 254.Em unsigned int . 255.It Cm O 256Equivalent to 257.Cm lo ; 258this exists for backwards compatibility. 259.It Cm u 260Matches an optionally signed decimal integer; 261the next pointer must be a pointer to 262.Em unsigned int . 263.It Cm x 264Matches an optionally signed hexadecimal integer; 265the next pointer must be a pointer to 266.Em unsigned int . 267.It Cm X 268Equivalent to 269.Cm x . 270.It Cm f 271Matches an optionally signed floating-point number; 272the next pointer must be a pointer to 273.Em float . 274.It Cm e 275Equivalent to 276.Cm f . 277.It Cm g 278Equivalent to 279.Cm f . 280.It Cm E 281Equivalent to 282.Cm f . 283.It Cm G 284Equivalent to 285.Cm f . 286.It Cm s 287Matches a sequence of non-white-space characters; 288the next pointer must be a pointer to 289.Em char , 290and the array must be large enough to accept all the sequence and the 291terminating 292.Dv NUL 293character. 294The input string stops at white space 295or at the maximum field width, whichever occurs first. 296.It Cm c 297Matches a sequence of 298.Em width 299count 300characters (default 1); 301the next pointer must be a pointer to 302.Em char , 303and there must be enough room for all the characters 304(no terminating 305.Dv NUL 306is added). 307The usual skip of leading white space is suppressed. 308To skip white space first, use an explicit space in the format. 309.It Cm \&[ 310Matches a nonempty sequence of characters from the specified set 311of accepted characters; 312the next pointer must be a pointer to 313.Em char , 314and there must be enough room for all the characters in the string, 315plus a terminating 316.Dv NUL 317character. 318The usual skip of leading white space is suppressed. 319The string is to be made up of characters in 320(or not in) 321a particular set; 322the set is defined by the characters between the open bracket 323.Cm [ 324character 325and a close bracket 326.Cm ] 327character. 328The set 329.Em excludes 330those characters 331if the first character after the open bracket is a circumflex 332.Cm ^ . 333To include a close bracket in the set, 334make it the first character after the open bracket 335or the circumflex; 336any other position will end the set. 337The hyphen character 338.Cm - 339is also special; 340when placed between two other characters, 341it adds all intervening characters to the set. 342To include a hyphen, 343make it the last character before the final close bracket. 344For instance, 345.Ql [^]0-9-] 346means the set `everything except close bracket, zero through nine, 347and hyphen'. 348The string ends with the appearance of a character not in the 349(or, with a circumflex, in) set 350or when the field width runs out. 351.It Cm p 352Matches a pointer value (as printed by 353.Ql %p 354in 355.Xr printf 3 ) ; 356the next pointer must be a pointer to 357.Em void . 358.It Cm n 359Nothing is expected; 360instead, the number of characters consumed thus far from the input 361is stored through the next pointer, 362which must be a pointer to 363.Em int . 364This is 365.Em not 366a conversion, although it can be suppressed with the 367.Cm * 368flag. 369.El 370.Pp 371For backwards compatibility, 372other conversion characters (except 373.Ql \e0 ) 374are taken as if they were 375.Ql %d 376or, if uppercase, 377.Ql %ld , 378and a `conversion' of 379.Ql %\e0 380causes an immediate return of 381.Dv EOF . 382.Sh RETURN VALUES 383These 384functions 385return 386the number of input items assigned, which can be fewer than provided 387for, or even zero, in the event of a matching failure. 388Zero 389indicates that, while there was input available, 390no conversions were assigned; 391typically this is due to an invalid input character, 392such as an alphabetic character for a 393.Ql %d 394conversion. 395The value 396.Dv EOF 397is returned if an input failure occurs before any conversion such as an 398end-of-file occurs. 399If an error or end-of-file occurs after conversion has begun, 400the number of conversions which were successfully completed is returned. 401.Sh SEE ALSO 402.Xr getc 3 , 403.Xr printf 3 , 404.Xr strtod 3 , 405.Xr strtol 3 , 406.Xr strtoul 3 407.Sh STANDARDS 408The functions 409.Fn fscanf , 410.Fn scanf , 411and 412.Fn sscanf 413conform to 414.St -isoC-90 . 415The 416.Cm %j , 417.Cm %t 418and 419.Cm %z 420conversion format modifiers 421conform to 422.St -isoC-99 . 423The 424.Fn vfscanf , 425.Fn vscanf 426and 427.Fn vsscanf 428functions conform to 429.St -isoC-99 . 430.Sh HISTORY 431The functions 432.Fn vscanf , 433.Fn vsscanf 434and 435.Fn vfscanf 436appeared in 437.Bx 4.4 438or even 439.Bx 4.3 . 440.Sh NOTES 441All of the backwards compatibility formats will be removed in the future. 442.Sh BUGS 443Numerical strings are truncated to 512 characters; for example, 444.Cm %f 445and 446.Cm %d 447are implicitly 448.Cm %512f 449and 450.Cm %512d . 451