getchar与putchar(借鉴别人的观点)

getchar函数,不需要参数,返回值是整型,功能是从标准输入的缓冲区“pop”出一个字符,而因为字符存储时是存储的字符的ASCII码,也就是一数字,所以getchar返回的整数就是这个字符的ASCII码;而因为C99要求getchar在遇到EOF时返回EOF,而EOF一般为-1,所以用char型数据类型无法标示,于是决定采用返回整型的实现方式。
7.19.7.6 The getchar function
Synopsis
1  #include <stdio.h>
    int getchar(void);
Description
2 The getchar function is equivalent to getc with the argument stdin.
Returns
3 The getchar function returns the next character from the input stream pointed to by
stdin. If the stream is at end-of-file, the end-of-file indicator for the stream is set and
getchar returns EOF. If a read error occurs, the error indicator for the stream is set and
getchar returns EOF.
putchar函数,需要一个整型参数,返回整型;执行时,函数将传入的整型参数类型转换为unsigned char,并将其写入标准输出的缓冲区,然后将这个字符返回,(或者是EOF),用整型的原因同上。
7.19.7.9 The putchar function
Synopsis
1 #include <stdio.h>
int putchar(int c);
Description
2 The putchar function is equivalent to putc with the second argument stdout.
Returns
3 The putchar function returns the character written. If a write error occurs, the error
indicator for the stream is set and putchar returns EOF.

正如上面C99文档中提到的,一般的实现中,getchar和putchar一般都是宏,最终的调用是fgetc和fputc,针对标准输入和标准输出。所有的操作仅对于缓冲区的一个字符,或者说一个字节这么大的数据。s

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值