[参考]C的scanf 和 C++的fscanf 的用法

说明:本文不适合新手学习,适合用来做参考。本文参考有其他博客的内容,不过年代久远已经忘记了,在此感谢各位博主!
scanf函数
用 法: int scanf(char *format[,argument,...]);          // scanf()函数是通用终端格式化输入函数,它从标准输入设备(键盘) 读取输入的信息。可以读入任何固有类型的数据并自动把数值变换成适当的机内格式。
调用格式:  scanf("<格式化字符串>",<地址表>);    //  scanf()函数返回成功赋值的数据项数,出错时则返回EOF。
其控制串由三类字符构成: 1、格式化说明符; 2、空白符; 3、非空白符;
1)格式化说明符
格式字符说明
%a、%A读入一个浮点值(仅C99有效) 
%f 、  %F、 %e、
%E、 %g 、 %G
读入一个浮点数
%c、%C 读入一个字符
%d读入十进制整数
%s读入一个字符串
%p读入一个指针
%u
读入一个无符号十进制整数
%n至此已读入值的等价字符数
%[]扫描字符集合
%%读%符号
%o读入八进制整数
%x 、%X读入十六进制整数
%i读入十进制,八进制,十六进制整数
2)空白字符
空白字符会使scanf()函数在读操作中略去输入中的一个或多个空白字符,空白符可以是space,tab,newline等等, 直到第一个非空白符出现为止。
3)非空白字符
一个非空白字符会使scanf()函数在读入时剔除掉与这个非空白字符相同的字符。
 
fscanf函数
format specifier  for  fscanf  follows this prototype:      [*][width][length]specifier 
The  format specifier  contain sub-specifiers:  asterisk  ( * ),  width  and  length  (in that order), which are optional and follow these specifications:
sub-specifierdescription
*An optional starting asterisk indicates that the data is to be read from the stream but ignored (i.e. it is not stored in the location pointed by an argument).   注:加上*可将该位置数据忽略
widthSpecifies the maximum number of characters to be read in the current reading operation (optional).
注:比如填入7.3,表示整数部分宽度为7,小数部分只读到第3位;也可以不填,不填则将对应数据不带舍入地读入
lengthOne of hhhllljztL (optional).
This alters the expected type of the storage pointed by the corresponding argument (see the last table).
Where the  specifier  character at the end is the most significant component, since it defines which characters are extracted, their interpretation and the type of its corresponding argument:
specifierDescriptionCharacters extracted
iuIntegerAny number of digits, optionally preceded by a sign (+ or -).
Decimal digits assumed by default (0-9), but a 0 prefix introduces octal digits (0-7), and 0x hexadecimal digits (0-f).
dDecimal integer
(十进制数)
Any number of decimal digits (0-9), optionally preceded by a sign (+ or -).
oOctal integerAny number of octal digits (0-7), optionally preceded by a sign (+ or -).
xHexadecimal integer (16进制数)Any number of hexadecimal digits (0-9a-fA-F), optionally preceded by 0x or 0X, and all optionally preceded by a sign (+ or -).
fegFloating point
number (浮点数)
A series of decimal digits, optionally containing a decimal point, optionally preceeded by a sign (+ or -) and optionally followed by the e or E character and a decimal integer (or some of the other sequences supported by strtod).
Implementations complying with C99 also support hexadecimal floating-point format when preceded by 0x or 0X.
a
cCharacterThe next character. If a width other than 1 is specified, the function reads exactlywidth characters and stores them in the successive locations of the array passed as argument. No null character is appended at the end.
sString of charactersAny number of non-whitespace characters, stopping at the first whitespacecharacter found. A terminating null character is automatically added at the end of the stored sequence.
pPointer addressA sequence of characters representing a pointer. The particular format used depends on the system and library implementation, but it is the same as the one used to format %p in fprintf.
[characters]ScansetAny number of the characters specified between the brackets.
A dash (-) that is not the first character may produce non-portable behavior in some library implementations.
[^characters]Negated scansetAny number of characters none of them specified as characters between the brackets.
nCountNo input is consumed.
The number of characters read so far from stream is stored in the pointed location.
%%% followed by another % matches a single %.
Except for  n , at least one character shall be consumed by any specifier. Otherwise the match fails, and the scan ends there.

This is a chart showing the types expected for the corresponding arguments where input is stored (both with and without a  length  sub-specifier):(length放在specifiers前会产生不同的效果,更具体。也可以选择不使用)
 specifiers
lengthd iu o xf e g ac s [] [^]pn
(none)int*unsigned int*float*char*void**int*
hhsigned char*unsigned char*   signed char*
hshort int*unsigned short int*   short int*
llong int*unsigned long int*double*wchar_t* long int*
lllong long int*unsigned long long int*   long long int*
jintmax_t*uintmax_t*   intmax_t*
zsize_t*size_t*   size_t*
tptrdiff_t*ptrdiff_t*   ptrdiff_t*
L  long double*   
Note:  Yellow rows indicate specifiers and sub-specifiers introduced by C99.
 
 
 
 

转载于:https://www.cnblogs.com/stxs/p/8675502.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值