名不副实的getchar()

名不副实的getchar()

函数命名通常遵守见名知意的规则,但getchar()貌似就是一个特例,函数名的字面意思即获取一个字符,然而它的返回类型不是char,而是int。

  • 头文件

    stdio.h

  • 函数原型

    int getchar(void);

  • 函数描述

       fgetc() reads the next character from stream and returns it as an
       unsigned char cast to an int, or EOF on end of file or error.

       getc() is equivalent to fgetc() except that it may be implemented
       as a macro which evaluates stream more than once.

       getchar() is equivalent to getc(stdin).
  • 返回值
       fgetc(), getc() and getchar() return the  character  read  as  an
       unsigned char cast to an int or EOF on end of file or error.
  • 实例

源代码

#include <stdio.h>

int main(void)
{
    int a = 0;

    a = getchar();

    printf("a : %d\n", a);

    return 0;
}

运行结果

[root@localhost lwp_workspace]# ./test
1
a : 49
[root@localhost lwp_workspace]# 

注意:字符‘1’的ascll即为数字49。

总结

getchar()返回获取字符的ascll或EOF。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值