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."

 

转载于:https://www.cnblogs.com/Patt/p/4945875.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值