getchar()函数用于从标准输入设备中读取一个字符(一个无符号字符),函数原型为:

#include <stdio.h>     int getchar(void);

返回值:该函数以无符号char强制转换为int的形式返回读取的字符,错误是返回EOF

练习1:获取一个字符功能实现

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int ch;
ch=getchar();
printf("ch:%c\n",ch);
exit(0);
}

 练习2:读取内核版本相关信息,实现代码方法。

#include <stdio.h>
#include <stdlib.h>
linclude <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char *argv[]){
char buf[512]= {0};
int fd;
int ret; /*打开文件*/
fd =open("/proc/version",O_RDONLY);if (fd == -1){
perror(""open error");exit(-1);
}
/*读取文件*/
ret = read(fd, buf, sizeof(buf));if (ret == -1) {
perror(""read error");exit(-1);
}
/*打印信息*/puts(buf);
/*关闭文件*/close(fd);
exit(0);
}