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