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 January 4, 2003 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 * restrict format" ... 56.Ft int 57.Fn fscanf "FILE * restrict stream" "const char * restrict format" ... 58.Ft int 59.Fn sscanf "const char * restrict str" "const char * restrict format" ... 60.In stdarg.h 61.Ft int 62.Fn vscanf "const char * restrict format" "va_list ap" 63.Ft int 64.Fn vsscanf "const char * restrict str" "const char * restrict format" "va_list ap" 65.Ft int 66.Fn vfscanf "FILE * restrict stream" "const char * restrict 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.Dv 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 ".Cm l No (ell)" 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 hh 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.Vt char 154(rather than 155.Vt int ) . 156.It Cm h 157Indicates that the conversion will be one of 158.Cm dioux 159or 160.Cm n 161and the next pointer is a pointer to a 162.Vt "short int" 163(rather than 164.Vt int ) . 165.It Cm l No (ell) 166Indicates that the conversion will be one of 167.Cm dioux 168or 169.Cm n 170and the next pointer is a pointer to a 171.Vt "long int" 172(rather than 173.Vt int ) , 174that the conversion will be one of 175.Cm a , e , f , 176or 177.Cm g 178and the next pointer is a pointer to 179.Vt double 180(rather than 181.Vt float ) , 182or that the conversion will be one of 183.Cm c , 184.Cm s 185or 186.Cm \&[ 187and the next pointer is a pointer to an array of 188.Vt wchar_t 189(rather than 190.Vt char ) . 191.It Cm ll No (ell ell) 192Indicates that the conversion will be one of 193.Cm dioux 194or 195.Cm n 196and the next pointer is a pointer to a 197.Vt "long long int" 198(rather than 199.Vt int ) . 200.It Cm L 201Indicates that the conversion will be one of 202.Cm a , e , f , 203or 204.Cm g 205and the next pointer is a pointer to 206.Vt "long double" . 207.It Cm j 208Indicates that the conversion will be one of 209.Cm dioux 210or 211.Cm n 212and the next pointer is a pointer to a 213.Vt intmax_t 214(rather than 215.Vt int ) . 216.It Cm t 217Indicates that the conversion will be one of 218.Cm dioux 219or 220.Cm n 221and the next pointer is a pointer to a 222.Vt ptrdiff_t 223(rather than 224.Vt int ) . 225.It Cm z 226Indicates that the conversion will be one of 227.Cm dioux 228or 229.Cm n 230and the next pointer is a pointer to a 231.Vt size_t 232(rather than 233.Vt int ) . 234.It Cm q 235(deprecated.) 236Indicates that the conversion will be one of 237.Cm dioux 238or 239.Cm n 240and the next pointer is a pointer to a 241.Vt "long long int" 242(rather than 243.Vt int ) . 244.El 245.Pp 246In addition to these flags, 247there may be an optional maximum field width, 248expressed as a decimal integer, 249between the 250.Cm % 251and the conversion. 252If no width is given, 253a default of 254.Dq infinity 255is used (with one exception, below); 256otherwise at most this many bytes are scanned 257in processing the conversion. 258In the case of the 259.Cm lc , 260.Cm ls 261and 262.Cm l[ 263conversions, the field width specifies the maximum number 264of multibyte characters that will be scanned. 265Before conversion begins, 266most conversions skip white space; 267this white space is not counted against the field width. 268.Pp 269The following conversions are available: 270.Bl -tag -width XXXX 271.It Cm % 272Matches a literal 273.Ql % . 274That is, 275.Dq Li %% 276in the format string 277matches a single input 278.Ql % 279character. 280No conversion is done, and assignment does not occur. 281.It Cm d 282Matches an optionally signed decimal integer; 283the next pointer must be a pointer to 284.Vt int . 285.It Cm i 286Matches an optionally signed integer; 287the next pointer must be a pointer to 288.Vt int . 289The integer is read in base 16 if it begins 290with 291.Ql 0x 292or 293.Ql 0X , 294in base 8 if it begins with 295.Ql 0 , 296and in base 10 otherwise. 297Only characters that correspond to the base are used. 298.It Cm o 299Matches an octal integer; 300the next pointer must be a pointer to 301.Vt "unsigned int" . 302.It Cm u 303Matches an optionally signed decimal integer; 304the next pointer must be a pointer to 305.Vt "unsigned int" . 306.It Cm x , X 307Matches an optionally signed hexadecimal integer; 308the next pointer must be a pointer to 309.Vt "unsigned int" . 310.It Cm a , A , e , E , f , F , g , G 311Matches a floating-point number in the style of 312.Xr strtod 3 . 313The next pointer must be a pointer to 314.Vt float 315(unless 316.Cm l 317or 318.Cm L 319is specified.) 320.It Cm s 321Matches a sequence of non-white-space characters; 322the next pointer must be a pointer to 323.Vt char , 324and the array must be large enough to accept all the sequence and the 325terminating 326.Dv NUL 327character. 328The input string stops at white space 329or at the maximum field width, whichever occurs first. 330.Pp 331If an 332.Cm l 333qualifier is present, the next pointer must be a pointer to 334.Vt wchar_t , 335into which the input will be placed after conversion by 336.Xr mbrtowc 3 . 337.It Cm S 338The same as 339.Cm ls . 340.It Cm c 341Matches a sequence of 342.Em width 343count 344characters (default 1); 345the next pointer must be a pointer to 346.Vt char , 347and there must be enough room for all the characters 348(no terminating 349.Dv NUL 350is added). 351The usual skip of leading white space is suppressed. 352To skip white space first, use an explicit space in the format. 353.Pp 354If an 355.Cm l 356qualifier is present, the next pointer must be a pointer to 357.Vt wchar_t , 358into which the input will be placed after conversion by 359.Xr mbrtowc 3 . 360.It Cm C 361The same as 362.Cm lc . 363.It Cm \&[ 364Matches a nonempty sequence of characters from the specified set 365of accepted characters; 366the next pointer must be a pointer to 367.Vt char , 368and there must be enough room for all the characters in the string, 369plus a terminating 370.Dv NUL 371character. 372The usual skip of leading white space is suppressed. 373The string is to be made up of characters in 374(or not in) 375a particular set; 376the set is defined by the characters between the open bracket 377.Cm [ 378character 379and a close bracket 380.Cm ] 381character. 382The set 383.Em excludes 384those characters 385if the first character after the open bracket is a circumflex 386.Cm ^ . 387To include a close bracket in the set, 388make it the first character after the open bracket 389or the circumflex; 390any other position will end the set. 391The hyphen character 392.Cm - 393is also special; 394when placed between two other characters, 395it adds all intervening characters to the set. 396To include a hyphen, 397make it the last character before the final close bracket. 398For instance, 399.Ql [^]0-9-] 400means the set 401.Dq "everything except close bracket, zero through nine, and hyphen" . 402The string ends with the appearance of a character not in the 403(or, with a circumflex, in) set 404or when the field width runs out. 405.Pp 406If an 407.Cm l 408qualifier is present, the next pointer must be a pointer to 409.Vt wchar_t , 410into which the input will be placed after conversion by 411.Xr mbrtowc 3 . 412.It Cm p 413Matches a pointer value (as printed by 414.Ql %p 415in 416.Xr printf 3 ) ; 417the next pointer must be a pointer to 418.Vt void . 419.It Cm n 420Nothing is expected; 421instead, the number of characters consumed thus far from the input 422is stored through the next pointer, 423which must be a pointer to 424.Vt int . 425This is 426.Em not 427a conversion, although it can be suppressed with the 428.Cm * 429flag. 430.El 431.Pp 432The decimal point 433character is defined in the program's locale (category 434.Dv LC_NUMERIC ) . 435.Pp 436For backwards compatibility, a 437.Dq conversion 438of 439.Ql %\e0 440causes an immediate return of 441.Dv EOF . 442.Sh RETURN VALUES 443These 444functions 445return 446the number of input items assigned, which can be fewer than provided 447for, or even zero, in the event of a matching failure. 448Zero 449indicates that, while there was input available, 450no conversions were assigned; 451typically this is due to an invalid input character, 452such as an alphabetic character for a 453.Ql %d 454conversion. 455The value 456.Dv EOF 457is returned if an input failure occurs before any conversion such as an 458end-of-file occurs. 459If an error or end-of-file occurs after conversion 460has begun, 461the number of conversions which were successfully completed is returned. 462.Sh SEE ALSO 463.Xr getc 3 , 464.Xr mbrtowc 3 , 465.Xr printf 3 , 466.Xr strtod 3 , 467.Xr strtol 3 , 468.Xr strtoul 3 , 469.Xr wscanf 3 470.Sh STANDARDS 471The functions 472.Fn fscanf , 473.Fn scanf , 474.Fn sscanf , 475.Fn vfscanf , 476.Fn vscanf 477and 478.Fn vsscanf 479conform to 480.St -isoC-99 . 481.Sh BUGS 482Earlier implementations of 483.Nm 484treated 485.Cm \&%D , \&%E , \&%F , \&%O 486and 487.Cm \&%X 488as their lowercase equivalents with an 489.Cm l 490modifier. 491In addition, 492.Nm 493treated an unknown conversion character as 494.Cm \&%d 495or 496.Cm \&%D , 497depending on its case. 498This functionality has been removed. 499.Pp 500Numerical strings are truncated to 512 characters; for example, 501.Cm %f 502and 503.Cm %d 504are implicitly 505.Cm %512f 506and 507.Cm %512d . 508.Pp 509The 510.Cm %n$ 511modifiers for positional arguments are not implemented. 512.Pp 513The 514.Nm 515family of functions do not correctly handle multibyte characters in the 516.Fa format 517argument. 518