大宇私人领地伊甸园丨宁愿做了后悔,也不要错过后悔[www.mrdayu.com]
注册

24小时联系邮箱:[email protected]

所有文章
大宇私人伊甸园: 首页 > 所有文章 > 编程人生 > C/C++语言 > C标准库详解 > getchar函数详解


友情链接:


getchar函数详解

发布时间:2018/12/07 C标准库详解 标签getchar阅读:29

函数原型:

extern int getchar(void);

头文件包含:

#include <stdio.h>

说明:

Reads the next character from stdin.
Equivalent to getc(stdin).

返回值:

The obtained character on success or EOF on failure.
If the failure has been caused by end-of-file condition,
additionally sets the eof indicator (see feof()) on stdin. 
If the failure has been caused by some other error,
sets the error indicator (see ferror()) on stdin.

Linux平台测试:

#include <stdio.h>
int main(){
        int c=0;
        printf("Press enter:\n");
        while((c=getchar())!='Q')
        {
                printf("key: %c\tvalue: %x",c,c);
                printf("\nPress enter:\n");
        }
        return 0;
}

运行:

[[email protected] STDC]$ gcc stdc_getchar.c
[[email protected] STDC]$ ./a.out 
Press enter:
s
key: s	value: 73
Press enter:
key: 
	value: a
Press enter:

Windows平台测试:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
	int ch;
	while ((ch = getchar()) != EOF)   /* read/print "abcde" from stdin */
		printf("%c", ch);

	/* Test reason for reaching EOF. */
	if (feof(stdin))          /* if failure caused by end-of-file condition */
		puts("End of file reached");
	else if (ferror(stdin))   /* if failure caused by some other error      */
	{
		perror("getchar()");
		fprintf(stderr, "getchar() failed in file %s at line # %d\n", __FILE__, __LINE__ - 9);
		exit(EXIT_FAILURE);
	}

	return EXIT_SUCCESS;
}

运行:

hello,dayu
hello,dayu
The bottom line, for me, is simple.

Let’s hope it doesn’t take 500 years for a discipline of testing to becomes the standard for software developers.

From:dayu

您好!请登录

合作网站快捷登录:
点击取消回复

已有0评论

    大宇博客

    点击这里给我发消息 点击这里给我发消息 点击这里给我发消息 点击这里给我发消息