C语言中的 getchar();什么意思~~各位给我介绍介绍下 好不好~谢谢~

grth0310 2010-11-22 06:57:46
getchar();什么意思,有什么用,怎样用法啊~
~~各位给我介绍介绍下 好不好~谢谢~
...全文
1123 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
s393102639 2010-12-06
  • 打赏
  • 举报
回复
《UNIX环境高级编程》 第二版 (中文版)
115页
T0Ols 2010-12-06
  • 打赏
  • 举报
回复
getchar 由宏实现:#define getchar() fgetc(stdin)。getchar有一个int型的返回值.当程序调用getchar时.程序就等着用户按键.用户输入的字符被存放在键盘缓冲区中.直到用户按回车为止(回车字符也放在缓冲区中).当用户键入回车之后,getchar才开始从stdio流中每次读入一个字符.getchar函数的返回值是用户输入的第一个字符的ASCII码,如出错返回-1,且将用户输入的字符回显到屏幕.如用户在按回车之前输入了不止一个字符,其他字符会保留在键盘缓存区中,等待后续getchar调用读取.也就是说,后续的getchar调用不会等待用户按键,而直接读取缓冲区中的字符,直到缓冲区中的字符读完为后,才等待用户按键.   getch与getchar基本功能相同,差别是getch直接从键盘获取键值,不等待用户按回车,只要用户按一个键,getch就立刻返回, getch返回值是用户输入的ASCII码,出错返回-1.输入的字符不会回显在屏幕上.getch函数常用于程序调试中,在调试时,在关键位置显示有关的结果以待查看,然后用getch函数暂停程序运行,当按任意键后程序继续运行.   这个版本忽略了个重点,getch()是非缓冲输入函数,就是不能用getch()来接受缓冲区已存在的字符,如以下C++程序,   int i;while(cin>>i);cin.clear();getchar();运行时如果输入1 2 3 a时必须用getchar()才能在后面程序获得正常输入,即使先前已经恢复流了,此处用getch()是万万不行的。   另外补充个函数,getche(),这个函数与前两上类似,功能也相近,都是输入一个字符,返回值同样是输入字符的ASCII码,但不同的是,此函数在输入后立即从控制台取字符,不以回车为结束(带回显)
赵4老师 2010-12-06
  • 打赏
  • 举报
回复
getc, getwc, getchar, getwchar
Read a character from a stream (getc, getwc), or get a character from stdin (getchar, getwchar).

int getc( FILE *stream );

wint_t getwc( FILE *stream );

int getchar( void );

wint_t getwchar( void );

Routine Required Header Compatibility
getc <stdio.h> ANSI, Win 95, Win NT
getwc <stdio.h> or <wchar.h> ANSI, Win 95, Win NT
getchar <stdio.h> ANSI, Win 95, Win NT
getwchar <stdio.h> or <wchar.h> ANSI, Win 95, Win NT


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version


Return Value

Each of these functions returns the character read. To indicate an read error or end-of-file condition, getc and getchar return EOF, and getwc and getwchar return WEOF. For getc and getchar, use ferror or feof to check for an error or for end of file.

Parameter

stream

Input stream

Remarks

Each of these routines reads a single character from a file at the current position and increments the associated file pointer (if defined) to point to the next character. In the case of getc and getwc, the file is associated with stream (see Choosing Between Functions and Macros). Routine-specific remarks follow.

Routine Remarks
getc Same as fgetc, but implemented as a function and as a macro.
getwc Wide-character version of getc. Reads a multibyte character or a wide character according to whether stream is opened in text mode or binary mode.
getchar Same as _fgetchar, but implemented as a function and as a macro.
getwchar Wide-character version of getchar. Reads a multibyte character or a wide character according to whether stream is opened in text mode or binary mode.


Generic-Text Routine Mappings

TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_gettc getc getc getwc
_gettchar getchar getchar getwchar


Example

/* GETC.C: This program uses getchar to read a single line
* of input from stdin, places this input in buffer, then
* terminates the string before printing it to the screen.
*/

#include <stdio.h>

void main( void )
{
char buffer[81];
int i, ch;

printf( "Enter a line: " );

/* Read in single line from "stdin": */
for( i = 0; (i < 80) && ((ch = getchar()) != EOF)
&& (ch != '\n'); i++ )
buffer[i] = (char)ch;

/* Terminate string with null character: */
buffer[i] = '\0';
printf( "%s\n", buffer );
}


Output

Enter a line: This is a test
This is a test


Stream I/O Routines

See Also fgetc, _getch, putc, ungetc
arkor 2010-12-05
  • 打赏
  • 举报
回复
我终于知道了,谢谢啊。
xy3213 2010-12-05
  • 打赏
  • 举报
回复
比如有这样的题目:输入一个字符判断一个字符串中,这个字符出现的次数
然而你输入的这一个字符就要用到getchar()函数
具体的用法如2楼
flysnowhite 2010-12-05
  • 打赏
  • 举报
回复
获取单个字符。
無_1024 2010-12-05
  • 打赏
  • 举报
回复
获取一个字符
xinyu_wen 2010-12-05
  • 打赏
  • 举报
回复
边看书、边打代码、理解记忆、要有思想、
这个问题还是看书的好、
cc_tao 2010-12-05
  • 打赏
  • 举报
回复
从键盘获取一个字符
GuoYingYang 2010-11-23
  • 打赏
  • 举报
回复
LZ好好看书,呵呵
hrx1989 2010-11-23
  • 打赏
  • 举报
回复
getchar()他的返回为什么会是一个int呀!!
sparklxd 2010-11-23
  • 打赏
  • 举报
回复
看书 看书
书上都有 讲很详细了
再去敲下键盘不就知道了
----------------------
我看你是只看书 不打代码的

++
bo_00 2010-11-22
  • 打赏
  • 举报
回复
getchar(3) - Linux man page
http://linux.die.net/man/3/getchar
無_1024 2010-11-22
  • 打赏
  • 举报
回复
接受一个字符 一般用于接受回车
ForestDB 2010-11-22
  • 打赏
  • 举报
回复
Google getchar
MSDN getchar
man getchar
dulongfirst 2010-11-22
  • 打赏
  • 举报
回复
从标准输入缓冲区获取一个字符
N35883653 2010-11-22
  • 打赏
  • 举报
回复
.....
ischarles 2010-11-22
  • 打赏
  • 举报
回复
getchar函数每次只接受一个字符,经常c=getchar()这样来使用
lelec 2010-11-22
  • 打赏
  • 举报
回复
从终端(或系统隐含指定的输入设备)输入一个字符。getchar函数没有参数。
hk2305621_1 2010-11-22
  • 打赏
  • 举报
回复
获取输入的字符,一次只能接收一个,并且以char返回值返回
加载更多回复(6)

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧