说明:本文不适合新手学习,适合用来做参考。本文参考有其他博客的内容,不过年代久远已经忘记了,在此感谢各位博主!
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-specifier description
* 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).   注:加上*可将该位置数据忽略
width Specifies the maximum number of characters to be read in the current reading operation (optional).
注:比如填入7.3,表示整数部分宽度为7,小数部分只读到第3位;也可以不填,不填则将对应数据不带舍入地读入
length One 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:

specifier Description Characters extracted
iu Integer Any 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).
d Decimal integer
(十进制数)
Any number of decimal digits (0-9), optionally preceded by a sign (+ or -).
o Octal integer Any number of octal digits (0-7), optionally preceded by a sign (+ or -).
x Hexadecimal 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 -).
feg Floating 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
c Character The 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.
s String of characters Any 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.
p Pointer address A 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] Scanset Any 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 scanset Any number of characters none of them specified as characters between the brackets.
n Count No 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
length d i u o x f e g a c s [] [^] p n
(none) int* unsigned int* float* char* void** int*
hh signed char* unsigned char*       signed char*
h short int* unsigned short int*       short int*
l long int* unsigned long int* double* wchar_t*   long int*
ll long long int* unsigned long long int*       long long int*
j intmax_t* uintmax_t*       intmax_t*
z size_t* size_t*       size_t*
t ptrdiff_t* ptrdiff_t*       ptrdiff_t*
L     long double*      

Note: Yellow rows indicate specifiers and sub-specifiers introduced by C99.

 
 
 
 

[参考]C的scanf 和 C++的fscanf 的用法的更多相关文章

  1. scanf/sscanf %[]格式控制串的用法(转)

    scanf/sscanf %[]格式控制串的用法 scanf中一种很少见但很有用的转换字符:[...]和[ ^...]. #include<stdio.h> int main() { ch ...

  2. sprintf()、fprintf()、fscanf()的用法

    sprintf函数的用法1.该函数包含在stdio.h的头文件中. 2.sprintf和平时我们常用的printf函数的功能很相似.sprintf函数打印到字符串中,而printf函数打印输出到屏幕上 ...

  3. fscanf的用法

    fscanf用于读取字符串数据流,遇到空白字符(空格' '; 制表符'\t'; 新行符'\n')就停止,若要读取完整的一行数据,可以使用格式控制("%[^\n]%*c"),或者使用 ...

  4. 关于while (~scanf("%d %d", &m, &n))的用法

    其功能是循环从输入流读入m和n,直到遇到EOF,有如下关系: while (~scanf("%d %d", &m, &n)) ↔ while (scanf(&quo ...

  5. scanf,fscanf,sscanf的区别

    scanf是从文件中读 sscanf是从字符串中读 scanf是从键盘输入中读   fread :以字节位计算长度,按照指定的长度和次数读取数据,遇到结尾或完成指定长度读取后停止.fscanf :格式 ...

  6. scanf,fscanf,sscanf的区别----整理

    转自原文 scanf,fscanf,sscanf的区别----整理 scanf 从控制台输入 fscanf 从文件输入 sscanf 从指定字符串输入 1.例:使用scanf函数输入数据. #incl ...

  7. 进程操作篇atexit execl exit fprintf fscanf getpid nice get priority printf setpid system vfork wait waitpid

    atexit(设置程序正常结束前调用的函数) 相关函数 _exit,exit,on_exit 表头文件 #include<stdlib.h> 定义函数 int atexit (void ( ...

  8. matlab fscanf用法

    matlab fscanf用法 matlab中的fscanf的用法如下: A=fscanf(fid,format)[A, count]=fscanf(fid,format,size) [A, coun ...

  9. fscanf函数的用法

    fscanf函数用法 简要介绍 fscanf()函数是格式化读写函数.它读取的对象是磁盘文件 函数原型: int fscanf(FILE * fp,char * format,...); 其中fp为文 ...

随机推荐

  1. 永久解决delphi 2010不能2次启动问题

    由于在Windows安装了最近的更新(KB2982791, KB2970228)后,Delphi的IDE需要创建的一个文件%TEMP%\EditorLineEnds.ttr会被系统锁定,导致除非重新启 ...

  2. MapReduce的核心编程思想

    1.MapReduce的核心编程思想 2.yarn集群工作机制 3.maptask并行度与决定机制 4.maptask工作机制 5.MapReduce整体流程 6.shuffle机制 7.yarn架构

  3. windbg遍历进程页表查看内存

    2016-12-09 近期想查看下系统分配了的页的页表项的标志位,但是发现资料较少,所以还是记录下,希望可以对某些朋友有所帮助! 系统:win7 32位虚拟机 平台:KVM虚拟化平台 win7 32位 ...

  4. Spring-基于设置函数的依赖注入

    Spring 基于设置函数的依赖注入 当容器调用一个无参的构造函数或一个无参的静态factory方法来初始化你的bean后,通过容器在你的bean上调用设值函数,基于设值函数的DI就完成了. 下面是T ...

  5. Docker中安装配置Oracle数据库

    本文使用的OS是Ubuntu([16.04.1_server][1])[注:Ubuntu是安装在vmware虚拟机上的]. 其他的Oracle连接工具:[sqldeveloper-4.1.5.21.7 ...

  6. 六、Mosquitto 高级应用之SSL/TLS

    mosquitto提供SSL支持加密的网络连接和身份验证.本章节讲述次功能的实现. 在此之前需要一些准备工作. 准本工作: 一台 Linux 服务器. 安装好 openssl (不会明白怎么安装 op ...

  7. 多个JS文件性能优化

    页面中引入的JS文件是阻塞式加载的,这样会影响页面性能.以下是JS文件性能优化方法: 一:将所有的<script>标签放到页面底部,也就是</body>闭合标签之前,这能确保在 ...

  8. Linux服务器上监控网络带宽命令

    本文介绍了一些可以用来监控网络使用情况的Linux命令行工具.这些工具可以监控通过网络接口传输的数据,并测量目前哪些数据所传输的速度.入站流量和出站流量分开来显示. 一些命令可以显示单个进程所使用的带 ...

  9. 错误:程序“XXX”不包含适合于入口点的静态“Main”方法

    解决办法: 1.右键类库属性 2.将输出类型改为“类库”

  10. PKU 1129 Channel Allocation(染色问题||搜索+剪枝)

    题目大意建模: 一个有N个节点的无向图,要求对每个节点进行染色,使得相邻两个节点颜色都不同,问最少需要多少种颜色? 那么题目就变成了一个经典的图的染色问题 例如:N=7 A:BCDEFG B:ACDE ...