📄 scanf.3
字号:
.\" @(#)scanf.3s 6.1 (Berkeley) 5/15/85.\".TH SCANF 3 "May 15, 1985".AT 3.SH NAMEscanf, fscanf, sscanf, vscanf, vfscanf, vsscanf \- formatted input conversion.SH SYNOPSIS.nf.ft B#include <stdio.h>#include <stdarg.h>int scanf(const char *\fIformat\fP \fR[\fP, \fIpointer\fP\fR] ...\fP)int fscanf(FILE *\fIstream\fP, const char *\fIformat\fP \fR[\fP, \fIpointer\fP\fR] ...\fP)int sscanf(const char *\fIs\fP, const char *\fIformat\fP \fR[\fP, \fIpointer\fP\fR] ...\fP)int vscanf(const char *\fIformat\fP, va_list \fIargs\fP)int vfscanf(FILE *\fIstream\fP, const char *\fIformat\fP, va_list \fIargs\fP)int vsscanf(const char *\fIs\fP, const char *\fIformat\fP, va_list \fIargs\fP).SH DESCRIPTION.B Scanfreads from the standard input stream.BR stdin ..B Fscanfreads from the named input.IR stream ..B Sscanfreads from the character string.IR s .Each function reads characters, interpretsthem according to a format, and stores the results in its arguments.Each expects as argumentsa control string.IR format ,described below,and a set of.I pointerargumentsindicating where the converted input should be stored..PPThe.B v*scanffunctions can be used to make functions like the first three by using the.BR stdarg (3)method to process the argument pointers..PPThecontrol stringusually containsconversion specifications, which are used to direct interpretationof input sequences.The control string may contain:.TP 41.Blanks, tabs or newlines,which match optional white space in the input..TP 42.An ordinary character (not %) which must matchthe next character of the input stream..TP 43.Conversion specifications, consisting of thecharacter.BR % ,an optional assignment suppressing character.BR * ,an optional numerical maximum field width, and a conversioncharacter..PPA conversion specification directs the conversion of thenext input field; the resultis placed in the variable pointed to by the corresponding argument,unless assignment suppression wasindicated by.BR * .An input field is defined as a string of non-space characters;it extends to the next inappropriate character or until the fieldwidth, if specified, is exhausted..PPThe conversion character indicates the interpretation of theinput field; the corresponding pointer argument mustusually be of a restricted type.The following conversion characters are legal:.TP 4.B %a single `%' is expectedin the input at this point;no assignment is done..TP 4.B da decimal integer is expected;the corresponding argument should be an integer pointer..TP 4.B oan octal integer is expected;the corresponding argument should be a integer pointer..TP 4.B xa hexadecimal integer is expected;the corresponding argument should be an integer pointer..ti -0.2i.TP 4.B sa character string is expected;the corresponding argument should be a character pointerpointing to an array of characters large enough to accept thestring and a terminating `\e0', which will be added.The input field is terminated by a space characteror a newline..TP 4.B ca character is expected; thecorresponding argument should be a character pointer.The normal skip over space characters is suppressedin this case;to read the next non-space character, try`%1s'.If a field width is given, the corresponding argumentshould refer to a character array, and theindicated number of characters is read..TP 4.B efga floating point number is expected;the next field is converted accordingly and stored through thecorresponding argument, which should be a pointer to a.BR float .The input format forfloating point numbers isan optionally signedstring of digitspossibly containing a decimal point, followed by an optionalexponent field consisting of an E or e followed by an optionally signed integer..TP 4.B [indicates a string not to be delimited by space characters.The left bracket is followed by a set of characters and a rightbracket; the characters between the brackets define a setof characters making up the string.If the first characteris not circumflex (\|^\|), the input fieldis all characters until the first character not in the set betweenthe brackets; if the first characterafter the left bracket is ^, the input field is all charactersuntil the first character which is in the remaining set of charactersbetween the brackets.The corresponding argument must point to a character array..PPThe conversion characters.BR d ,.B oand.B xmay be capitalized or preceded by.B lto indicate that a pointer to.B longrather than to.B intis in the argument list.Similarly, the conversion characters.BR e ,.B for.B gmay be capitalized orpreceded by.B lto indicate a pointer to .B doublerather than to .BR float .The conversion characters.BR d ,.B oand.B xmay be preceded by.B hto indicate a pointer to.B shortrather than to.BR int ..PPThe.B scanffunctions return the number of successfully matched and assigned inputitems.This can be used to decide how many input items were found.The constant.SM.B EOFis returned upon end of input; note that this is differentfrom 0, which means that no conversion was done;if conversion was intended, it was frustrated by aninappropriate character in the input..PPFor example, the call.IP "\&" 10int i; float x; char name[50];.brscanf("%d%f%s", &i, &x, name);.PPwith the input line.IP25 54.32E\(mi1 thompson.PPwill assign to.B ithe value25,.B xthe value 5.432, and.B namewill contain `\fBthompson\e0\fP' .Or,.IPint i; float x; char name[50];.brscanf("%2d%f%*d%[1234567890]", &i, &x, name);.PPwith input.IP56789 0123 56a72.PPwill assign 56 to.BR i ,789.0 to.BR x ,skip `0123',and place the string `56\e0' in.BR name .The next call to.B getcharwill return `a'..SH "SEE ALSO".BR atof (3),.BR getc (3),.BR printf (3),.BR stdarg (3)..SH DIAGNOSTICSThe .B scanffunctions return.SM.B EOFon end of input,and a short count for missing or illegal data items..SH BUGSThe success of literal matches and suppressedassignments is not directlydeterminable.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -