getchar() 和 scanf("%c")的区别
getchar()和scanf("%c")的功能都是从STDIN读一个字符,单论功能两者没有区别。
但两者的返回值是有区别的:
------------------------------------------------
scanf()的详尽介绍请移步这里。
-------------------------------------------------
scanf()的返回值的含义是:
On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.
If a reading error happens or the end-of-file is reached while reading, the proper indicator is set (feof or ferror). And, if either happens before any data could be successfully read, EOF is returned.
(自http://www.cplusplus.com/reference/cstdio/scanf/?kw=scanf)
putchar()返回值的含义是:
On success, the character read is returned (promoted to an int value).
The return type is int to accommodate for the special value EOF, which indicates failure: (这句话极为重要,请反复阅读)
If the standard input was at the end-of-file, the function returns EOF and sets the eof indicator (feof) of stdin.
If some other reading error happens, the function also returns EOF, but sets its error indicator (ferror) instead.
(自http://www.cplusplus.com/reference/cstdio/getchar/?kw=getchar)
getchar()有一个隐藏的坑,极不容易发现:
for(char ch; ch=getchar(), ch!=EOF; ){
//...
}
先来看一下EOF的定义:
It is a macro definition of type int that expands into a negative integral constant expression (generally, -1).
It is used as the value returned by several functions in header <cstdio> to indicate that the End-of-File has been reached or to signal some other failure conditions.
It is also used as the value to represent an invalid character.
In C++, this macro corresponds to the value of char_traits<char>::eof().
----------------------------------------------------------------------------------------------------------
考虑表达式
ch = getchar()
char只有8位,这意味着,只有getchar()返回0(00)~255(FF)时,char才能完全接受。ASCII码只有7位 (0(00) ~ 127(7F)),存得下。
如果某个字符的编码为255(FF)(扩展ASCII码),EOF又恰好为-1,就无法区分EOF和这个字符,导致错误。
用scanf(“%c")就不会有这样的问题。
总之,
scanf("%c", &ch) == EOF
成立的话,一定是遇到的EOF,换作getchar()就不一定。
-------------------------------------------------------------------------------------------------------------------
ZOJ 3439是个极好的例子
这个坑爹的题目里有这样一句坑爹的话
" The test input just contains all one-byte characters."
getchar() 和 scanf("%c")的区别的更多相关文章
- C/C++ scanf和gets 区别 , printf和puts区别
ref 1. scanf和gets区别 | 博客园 2. printf和puts区别 | CSDN scanf和gets都能从输入流stdin读取字符串,那么它们有什么区别呢? scanf 留回车:开 ...
- getchar、scanf以及缓冲区的概念
1.getchar()是stdio.h中的库函数,它的作用是从stdin流中读入一个字符,也就是说,如果stdin有数据的话不用输入它就可以直接读取了. getch()和getche( ...
- getchar与scanf区别
scanf可以一次按照设定的输入格式输入多个变量数据.如int d,float f,char str[20],scanf("%d%f%s",d,f,str); getchar()只 ...
- getchar(),gets(),scanf()的差异比较
scanf( )函数和gets( )函数都可用于输入字符串,但在功能上有区别.若想从键盘上输入字符串"hi hello",则应该使用gets()函数. gets可以接收空格:而sc ...
- scanf gets fgets区别与联系 puts fputs printf区别与联系
组一:scanf( )函数 gets( )函数 fgets()函数都可用于输入字符串, 组二:printf( )函数 puts( )函数 fputs()函数则用于字符串的输出. 两组内部函数各有 ...
- 【C语言】gets()和scanf()函数的区别
scanf函数与gets函数 scanf函数和gets( )函数都可用于输入字符串,但在功能上有区别.若想从键盘上输入字符串"hi hello",则应该使用gets函数. gets ...
- getchar() 、 scanf() 、流与缓冲区
C中的缓冲区一直是debug的重灾区,今天在写一个命令行界面的时候又遇到了这个问题,所以来总结一波. 两函数的不同之处 scanf() 会把 stdinBuff 中的特定格式数据取出,非特定格式数据则 ...
- C Language Study - gets , getchar & scanf
慢慢的发现C语言功底是如此的薄弱,被这几个字符输入函数搞糊涂了又~~ 来,再来忧伤一次吧~ 那么.我们从scanf開始: 假如说你要将一串字符输入到一字符数组里,例如以下面程序, char a[2]; ...
- scanf("%c\n",&a)和scanf("%c",&a)区别
scanf("%c",&a); 当输入字符的时候,我们按下任意字符 + 回车的时候,回车没有被当作为分隔符,而是作为一个转义字符与输入的字符一起保存在缓存区.第一次scan ...
随机推荐
- Android TextView中文字通过SpannableString来设置超链接、颜色、字体等属性
在Android中,TextView是我们最常用的用来显示文本的控件. 一般情况下,TextView中的文本都是一个样式.那么如何对于TextView中各个部分的文本来设置字体,大小,颜色,样式,以及 ...
- Sublime Text 2 - 性感无比的代码编辑器!程序员必备神器!跨平台支持Win/Mac/Linux
我用过的编辑器不少,真不少- 但却没有哪款让我特别心仪的,直到我遇到了 Sublime Text 2 !如果说“神器”是我能给予一款软件最高的评价,那么我很乐意为它封上这么一个称号.它小巧绿色且速度非 ...
- 九度oj-1003-Java
题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开. 现在请计算A+B的结果,并以正常形式输出. 输入: 输入包含多组数据数据,每组数据占一行,由两 ...
- Java并发编程-CopyOnWriteArrayList
CopyOnWriteArrayList原理 首先每次写操作,都将数组copy一份,并赋值给arrays 读操作读不加锁 写操作加锁 ReentrantLock 因为每次写都要copy数组,这是一项繁 ...
- jQuery ajax - get(),getJSON(),post()方法
1) jQuery ajax - get() 方法: $(selector).get(url,data,success(response,status,xhr),dataType) 参数 ...
- js的基本的一些方法
我们不是要背诵东西,只是因为这是我们生存的技能. 加油吧少年! 1.函数的块级作用域和函数的自我执行是一回事.!(function () { function box(){alert('hello') ...
- [USACO2004][poj1985]Cow Marathon(2次bfs求树的直径)
http://poj.org/problem?id=1985 题意:就是给你一颗树,求树的直径(即问哪两点之间的距离最长) 分析: 1.树形dp:只要考虑根节点和子节点的关系就可以了 2.两次bfs: ...
- WPF MenuItem 四种角色分析
Menu Menu的样式很简单,就是顶部的那个框,如下图 而其中的文字“文件”“图形”...是属于MenuItem的,要灵活使用MenuItem,就需要了解MenuItem.Role的作用 T ...
- 每天一个linux命令(20):linux chmod命令
chmod命令用于改变linux系统文件或目录的访问权限.用它控制文件或目录的访问权限.该命令有两种用法.一种是包含字母和操作符表达式的文字设定法:另一种是包含数字的数字设定法. Linux系统中的每 ...
- 使用git管理代码的心得
一.简易使用流程 首先下载安装git,点击Git Bash进入编辑界面,之后如下图进入目录并通过命令 git init 把这个目录变成git可以管理的仓库 接下来使用git add .命令将所有文件添 ...