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