首页 > “int getchar(void) 函数从屏幕读取下一个可用的字符,并把它返回为一个整数”,真的只能返回整数吗?

“int getchar(void) 函数从屏幕读取下一个可用的字符,并把它返回为一个整数”,真的只能返回整数吗?

#include <stdio.h>
int main( )
{
   int c;

   printf( "Enter a value :");
   c = getchar( );

   printf( "\nYou entered: ");
   putchar( c );

   return 0;
}

比如上面这段代码,来自:http://www.runoob.com/cprogramming/c-input-output.html

返回的结果明显可以是字母,为什么用int c,而不是char c[1] ?


????????????


已经找到答案了:
http://zhidao.baidu.com/link?url=vk1oQy0xKiIvq1Od-SraqX35BQmRf7Mf2zUv7ucVO5MvPvT_lsJSGUk3K4CnXOrGTlx6AaTpTOhhnWxbddPK6-JrCriGUTysa-q1HdCHOs7


好歹也别贴一个 baidu知道的答案啊.

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.

getchar() 可以返回 EOF, 一般是 -1. char 为 ascii, 范围 0 - 255. 没有办法返回-1了.

【热门文章】
【热门文章】