1.\" $NetBSD: scanf.3,v 1.8 1998/02/05 18:49:32 perry 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. All advertising materials mentioning features or use of this software 19.\" must display the following acknowledgement: 20.\" This product includes software developed by the University of 21.\" California, Berkeley and its contributors. 22.\" 4. Neither the name of the University nor the names of its contributors 23.\" may be used to endorse or promote products derived from this software 24.\" without specific prior written permission. 25.\" 26.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36.\" SUCH DAMAGE. 37.\" 38.\" @(#)scanf.3 8.2 (Berkeley) 12/11/93 39.\" 40.Dd January 31, 1995 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.Fd #include <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.Fd #include <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 `suppression' below). 118All conversions are introduced by the 119.Cm % 120(percent sign) character. 121The 122.Fa format 123string 124may also contain other characters. 125White space (such as blanks, tabs, or newlines) in the 126.Fa format 127string match any amount of white space, including none, in the input. 128Everything else 129matches only itself. 130Scanning stops 131when an input character does not match such a format character. 132Scanning also stops 133when an input conversion cannot be made (see below). 134.Sh CONVERSIONS 135Following the 136.Cm % 137character introducing a conversion 138there may be a number of 139.Em flag 140characters, as follows: 141.Bl -tag -width indent 142.It Cm * 143Suppresses assignment. 144The conversion that follows occurs as usual, but no pointer is used; 145the result of the conversion is simply discarded. 146.It Cm h 147Indicates that the conversion will be one of 148.Cm dioux 149or 150.Cm n 151and the next pointer is a pointer to a 152.Em short int 153(rather than 154.Em int ) . 155.It Cm l 156Indicates either that the conversion will be one of 157.Cm dioux 158or 159.Cm n 160and the next pointer is a pointer to a 161.Em long int 162(rather than 163.Em int ) , 164or that the conversion will be one of 165.Cm efg 166and the next pointer is a pointer to 167.Em double 168(rather than 169.Em float ) . 170.It Cm q 171Indicates that the conversion will be one of 172.Cm dioux 173or 174.Cm n 175and the next pointer is a pointer to a 176.Em quad_t 177(rather than 178.Em int ) . 179.It Cm L 180Indicates that the conversion will be 181.Cm efg 182and the next pointer is a pointer to 183.Em long double . 184.El 185.Pp 186In addition to these flags, 187there may be an optional maximum field width, 188expressed as a decimal integer, 189between the 190.Cm % 191and the conversion. 192If no width is given, 193a default of `infinity' is used (with one exception, below); 194otherwise at most this many characters are scanned 195in processing the conversion. 196Before conversion begins, 197most conversions skip white space; 198this white space is not counted against the field width. 199.Pp 200The following conversions are available: 201.Bl -tag -width XXXX 202.It Cm % 203Matches a literal `%'. 204That is, `%\&%' in the format string 205matches a single input `%' character. 206No conversion is done, and assignment does not occur. 207.It Cm d 208Matches an optionally signed decimal integer; 209the next pointer must be a pointer to 210.Em int . 211.It Cm D 212Equivalent to 213.Cm ld ; 214this exists only for backwards compatibility. 215.It Cm i 216Matches an optionally signed integer; 217the next pointer must be a pointer to 218.Em int . 219The integer is read in base 16 if it begins 220with 221.Ql 0x 222or 223.Ql 0X , 224in base 8 if it begins with 225.Ql 0 , 226and in base 10 otherwise. 227Only characters that correspond to the base are used. 228.It Cm o 229Matches an octal integer; 230the next pointer must be a pointer to 231.Em unsigned int . 232.It Cm O 233Equivalent to 234.Cm lo ; 235this exists for backwards compatibility. 236.It Cm u 237Matches an optionally signed decimal integer; 238the next pointer must be a pointer to 239.Em unsigned int . 240.It Cm x 241Matches an optionally signed hexadecimal integer; 242the next pointer must be a pointer to 243.Em unsigned int . 244.It Cm X 245Equivalent to 246.Cm x . 247.It Cm f 248Matches an optionally signed floating-point number; 249the next pointer must be a pointer to 250.Em float . 251.It Cm e 252Equivalent to 253.Cm f . 254.It Cm g 255Equivalent to 256.Cm f . 257.It Cm E 258Equivalent to 259.Cm f . 260.It Cm G 261Equivalent to 262.Cm f . 263.It Cm s 264Matches a sequence of non-white-space characters; 265the next pointer must be a pointer to 266.Em char , 267and the array must be large enough to accept all the sequence and the 268terminating 269.Dv NUL 270character. 271The input string stops at white space 272or at the maximum field width, whichever occurs first. 273.It Cm c 274Matches a sequence of 275.Em width 276count 277characters (default 1); 278the next pointer must be a pointer to 279.Em char , 280and there must be enough room for all the characters 281(no terminating 282.Dv NUL 283is added). 284The usual skip of leading white space is suppressed. 285To skip white space first, use an explicit space in the format. 286.It Cm \&[ 287Matches a nonempty sequence of characters from the specified set 288of accepted characters; 289the next pointer must be a pointer to 290.Em char , 291and there must be enough room for all the characters in the string, 292plus a terminating 293.Dv NUL 294character. 295The usual skip of leading white space is suppressed. 296The string is to be made up of characters in 297(or not in) 298a particular set; 299the set is defined by the characters between the open bracket 300.Cm [ 301character 302and a close bracket 303.Cm ] 304character. 305The set 306.Em excludes 307those characters 308if the first character after the open bracket is a circumflex 309.Cm ^ . 310To include a close bracket in the set, 311make it the first character after the open bracket 312or the circumflex; 313any other position will end the set. 314The hyphen character 315.Cm - 316is also special; 317when placed between two other characters, 318it adds all intervening characters to the set. 319To include a hyphen, 320make it the last character before the final close bracket. 321For instance, 322.Ql [^]0-9-] 323means the set `everything except close bracket, zero through nine, 324and hyphen'. 325The string ends with the appearance of a character not in the 326(or, with a circumflex, in) set 327or when the field width runs out. 328.It Cm p 329Matches a pointer value (as printed by 330.Ql %p 331in 332.Xr printf 3 ) ; 333the next pointer must be a pointer to 334.Em void . 335.It Cm n 336Nothing is expected; 337instead, the number of characters consumed thus far from the input 338is stored through the next pointer, 339which must be a pointer to 340.Em int . 341This is 342.Em not 343a conversion, although it can be suppressed with the 344.Cm * 345flag. 346.El 347.Pp 348For backwards compatibility, 349other conversion characters (except 350.Ql \e0 ) 351are taken as if they were 352.Ql %d 353or, if uppercase, 354.Ql %ld , 355and a `conversion' of 356.Ql %\e0 357causes an immediate return of 358.Dv EOF . 359.Pp 360.Sh RETURN VALUES 361These 362functions 363return 364the number of input items assigned, which can be fewer than provided 365for, or even zero, in the event of a matching failure. 366Zero 367indicates that, while there was input available, 368no conversions were assigned; 369typically this is due to an invalid input character, 370such as an alphabetic character for a 371.Ql %d 372conversion. 373The value 374.Dv EOF 375is returned if an input failure occurs before any conversion such as an 376end-of-file occurs. If an error or end-of-file occurs after conversion 377has begun, 378the number of conversions which were successfully completed is returned. 379.Sh SEE ALSO 380.Xr strtol 3 , 381.Xr strtoul 3 , 382.Xr strtod 3 , 383.Xr getc 3 , 384.Xr printf 3 385.Sh STANDARDS 386The functions 387.Fn fscanf , 388.Fn scanf , 389and 390.Fn sscanf 391conform to 392.St -ansiC . 393.Sh HISTORY 394The functions 395.Fn vscanf , 396.Fn vsscanf 397and 398.Fn vfscanf 399are new to this release. 400.Sh BUGS 401All of the backwards compatibility formats will be removed in the future. 402.Pp 403Numerical strings are truncated to 512 characters; for example, 404.Cm %f 405and 406.Cm %d 407are implicitly 408.Cm %512f 409and 410.Cm %512d . 411