📄 scanf.3
字号:
.\" Copyright (c) 1990, 1991, 1993.\" The Regents of the University of California. All rights reserved..\".\" This code is derived from software contributed to Berkeley by.\" Chris Torek and the American National Standards Committee X3,.\" on Information Processing Systems..\".\" Redistribution and use in source and binary forms, with or without.\" modification, are permitted provided that the following conditions.\" are met:.\" 1. Redistributions of source code must retain the above copyright.\" notice, this list of conditions and the following disclaimer..\" 2. Redistributions in binary form must reproduce the above copyright.\" notice, this list of conditions and the following disclaimer in the.\" documentation and/or other materials provided with the distribution..\" 3. All advertising materials mentioning features or use of this software.\" must display the following acknowledgement:.\" This product includes software developed by the University of.\" California, Berkeley and its contributors..\" 4. Neither the name of the University nor the names of its contributors.\" may be used to endorse or promote products derived from this software.\" without specific prior written permission..\".\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION).\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF.\" SUCH DAMAGE..\".\" @(#)scanf.3 8.2 (Berkeley) 12/11/93.\".Dd December 11, 1993.Dt SCANF 3.Os.Sh NAME.Nm scanf ,.Nm fscanf ,.Nm sscanf ,.Nm vscanf ,.Nm vsscanf ,.Nm vfscanf.Nd input format conversion.Sh SYNOPSIS.Fd #include <stdio.h>.Ft int.Fn scanf "const char *format" ....Ft int.Fn fscanf "FILE *stream" "const char *format" ....Ft int.Fn sscanf "const char *str" "const char *format" ....Fd #include <stdarg.h>.Ft int.Fn vscanf "const char *format" "va_list ap".Ft int.Fn vsscanf "const char *str" "const char *format" "va_list ap".Ft int.Fn vfscanf "FILE *stream" "const char *format" "va_list ap".Sh DESCRIPTIONThe.Fn scanffamily of functions scans input according to a.Fa formatas described below.This format may contain.Em conversion specifiers ;the results from such conversions, if any,are stored through the.Em pointerarguments.The.Fn scanffunctionreads input from the standard input stream.Em stdin ,.Fn fscanfreads input from the stream pointer.Fa stream ,and.Fn sscanfreads its input from the character string pointed to by.Fa str .The.Fn vfscanffunctionis analogous to.Xr vfprintf 3and reads input from the stream pointer.Fa streamusing a variable argument list of pointers (see.Xr stdarg 3 ) .The.Fn vscanffunction scans a variable argument list from the standard input andthe.Fn vsscanffunction scans it from a string;these are analogous tothe.Fn vprintfand.Fn vsprintffunctions respectively.Each successive.Em pointerargument must correspond properly witheach successive conversion specifier(but see `suppression' below).All conversions are introduced by the.Cm %(percent sign) character.The.Fa formatstringmay also contain other characters.White space (such as blanks, tabs, or newlines) in the.Fa formatstring match any amount of white space, including none, in the input.Everything elsematches only itself.Scanning stopswhen an input character does not match such a format character.Scanning also stopswhen an input conversion cannot be made (see below)..Sh CONVERSIONSFollowing the.Cm %character introducing a conversionthere may be a number of.Em flagcharacters, as follows:.Bl -tag -width indent.It Cm *Suppresses assignment.The conversion that follows occurs as usual, but no pointer is used;the result of the conversion is simply discarded..It Cm hIndicates that the conversion will be one of.Cm diouxor.Cm nand the next pointer is a pointer to a.Em short int(rather than.Em int ) ..It Cm lIndicates either that the conversion will be one of.Cm diouxor.Cm nand the next pointer is a pointer to a.Em long int(rather than.Em int ) ,or that the conversion will be one of.Cm efgand the next pointer is a pointer to.Em double(rather than.Em float ) ..It Cm LIndicates that the conversion will be.Cm efgand the next pointer is a pointer to.Em long double .(This type is not implemented; the.Cm Lflag is currently ignored.).El.PpIn addition to these flags,there may be an optional maximum field width,expressed as a decimal integer,between the.Cm %and the conversion.If no width is given,a default of `infinity' is used (with one exception, below);otherwise at most this many characters are scannedin processing the conversion.Before conversion begins,most conversions skip white space;this white space is not counted against the field width..PpThe following conversions are available:.Bl -tag -width XXXX.It Cm %Matches a literal `%'.That is, `%\&%' in the format stringmatches a single input `%' character.No conversion is done, and assignment does not occur..It Cm dMatches an optionally signed decimal integer;the next pointer must be a pointer to.Em int ..It Cm DEquivalent to.Xr ld ;this exists only for backwards compatibility..It Cm iMatches an optionally signed integer;the next pointer must be a pointer to.Em int .The integer is read in base 16 if it beginswith.Ql 0xor.Ql 0X ,in base 8 if it begins with.Ql 0 ,and in base 10 otherwise.Only characters that correspond to the base are used..It Cm oMatches an octal integer;the next pointer must be a pointer to.Em unsigned int ..It Cm OEquivalent to.Xr lo ;this exists for backwards compatibility..It Cm uMatches an optionally signed decimal integer;the next pointer must be a pointer to.Em unsigned int ..It Cm xMatches an optionally signed hexadecimal integer;the next pointer must be a pointer to.Em unsigned int ..It Cm XEquivalent to.Cm lx ;this violates the.St -ansiC ,but is backwards compatible with previous.Uxsystems..It Cm fMatches an optionally signed floating-point number;the next pointer must be a pointer to.Em float ..It Cm eEquivalent to.Cm f ..It Cm gEquivalent to.Cm f ..It Cm EEquivalent to.Cm lf ;this violates the.St -ansiC ,but is backwards compatible with previous.Uxsystems..It Cm FEquivalent to.Cm lf ;this exists only for backwards compatibility..It Cm sMatches a sequence of non-white-space characters;the next pointer must be a pointer to.Em char ,and the array must be large enough to accept all the sequence and theterminating.Dv NULcharacter.The input string stops at white spaceor at the maximum field width, whichever occurs first..It Cm cMatches a sequence of.Em widthcountcharacters (default 1);the next pointer must be a pointer to.Em char ,and there must be enough room for all the characters(no terminating.Dv NULis added).The usual skip of leading white space is suppressed.To skip white space first, use an explicit space in the format..It Cm \&[Matches a nonempty sequence of characters from the specified setof accepted characters;the next pointer must be a pointer to.Em char ,and there must be enough room for all the characters in the string,plus a terminating.Dv NULcharacter.The usual skip of leading white space is suppressed.The string is to be made up of characters in(or not in)a particular set;the set is defined by the characters between the open bracket.Cm [characterand a close bracket.Cm ]character.The set.Em excludesthose charactersif the first character after the open bracket is a circumflex.Cm ^ .To include a close bracket in the set,make it the first character after the open bracketor the circumflex;any other position will end the set.The hyphen character.Cm -is also special;when placed between two other characters,it adds all intervening characters to the set.To include a hyphen,make it the last character before the final close bracket.For instance,.Ql [^]0-9-]means the set `everything except close bracket, zero through nine,and hyphen'.The string ends with the appearance of a character not in the(or, with a circumflex, in) setor when the field width runs out..It Cm pMatches a pointer value (as printed by.Ql %pin.Xr printf 3 ) ;the next pointer must be a pointer to.Em void ..It Cm nNothing is expected;instead, the number of characters consumed thus far from the inputis stored through the next pointer,which must be a pointer to.Em int .This is.Em nota conversion, although it can be suppressed with the.Cm *flag..El.PpFor backwards compatibility,other conversion characters (except.Ql \e0 )are taken as if they were.Ql %dor, if uppercase,.Ql %ld ,and a `conversion' of.Ql %\e0causes an immediate return of.Dv EOF .The.Cm Fand.Cm Xconversions will be changed in the futureto conform to the.Tn ANSIC standard,after which they will act like.Cm fand.Cm xrespectively..Pp.Sh RETURN VALUESThesefunctionsreturnthe number of input items assigned, which can be fewer than providedfor, or even zero, in the event of a matching failure.Zeroindicates that, while there was input available,no conversions were assigned;typically this is due to an invalid input character,such as an alphabetic character for a.Ql %dconversion.The value.Dv EOFis returned if an input failure occurs before any conversion such as anend-of-file occurs. If an error or end-of-file occurs after conversionhas begun,the number of conversions which were successfully completed is returned..Sh SEE ALSO.Xr strtol 3 ,.Xr strtoul 3 ,.Xr strtod 3 ,.Xr getc 3 ,.Xr printf 3.Sh STANDARDSThe functions.Fn fscanf ,.Fn scanf ,and.Fn sscanfconform to.St -ansiC ..Sh HISTORYThe functions.Fn vscanf ,.Fn vsscanfand.Fn vfscanfare new to this release..Sh BUGSThe current situation with.Cm %Fand.Cm %Xconversions is unfortunate..PpAll of the backwards compatibility formats will be removed in the future..PpNumerical strings are truncated to 512 characters; for example,.Cm %fand.Cm %dare implicitly.Cm %512fand.Cm %512d .
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -