名不副实的getchar()

来源:互联网 发布:三大男高音 知乎 编辑:程序博客网 时间:2024/04/30 03:22

名不副实的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]# ./test1a : 49[root@localhost lwp_workspace]# 

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

总结

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

0 0
原创粉丝点击