C语言中的——EOF————“多组输入”中的应用(第12篇)

零、EOF :end of file 文件结束标志

一、EOF = -1

#include <stdio.h>

int main()
{
	printf("EOF = %d\n", EOF);
	return 0;
}

在这里插入图片描述

二、EOF 的来源

打开 https://cplusplus.com/ 网站 (使用 旧版),搜索 scanf 函数

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.

If a reading error happens or the end-of-file is reached while reading, the proper indicator is set (feof or ferror). And, if either happens before any data could be successfully read, EOF is returned.

If an encoding error happens interpreting wide characters, the function sets errno to EILSEQ.

如果成功,函数返回参数列表中成功填充的项数。由于匹配失败、读取错误或到达文件末尾,此计数可以匹配预期的项数,也可以更少(甚至为零)。

如果在读取过程中发生读取错误或到达文件末尾,则设置适当的指示器(feof或ferror)。并且,如果在成功读取任何数据之前发生任何一种情况,则返回EOF。

如果在解释宽字符时发生编码错误,函数将errno设置为EILSEQ。

1. scanf 函数 返回的是 读取到的数据的个数

int main()
{
	int a = 0;
	int b = scanf("%d", &a);
	printf("%d\n", b);
	return 0;
}

在这里插入图片描述

int main()
{
	int a = 0;
	int b = 0;
	int c = scanf("%d %d", &a, &b);
	printf("%d\n", c);
	return 0;
}

在这里插入图片描述

2. 如果 scanf 函数读取失败,会返回 EOF

3. CTRL + Z 让程序停下

(下面 典型例题 有详细讲解)

由于VS编译器存在漏洞,需要连续按 三次 CTRL + Z,才能让程序停止。
在其他编译器下,正常按一次就可以了。

int main()
{
	int a = 0;
	while (scanf("%d", &a)!= EOF)
	{
		printf("ok\n");
	}
	return 0;
}

在这里插入图片描述

三、典型例题

  1. 题目:
    描述
    据说智商140以上者称为天才,KiKi想知道他自己是不是天才,请帮他编程判断。输入一个整数表示一个人的智商,如果大于等于140,则表明他是一个天才,输出“Genius”。

    输入描述:
    多组输入,每行输入包括一个整数表示的智商。

    输出描述:
    针对每行输入,输出“Genius”。

    问题关键:多组输入

    然而,我的答案是:单次输入

int main()
{
	int iq = 0;
	int a = 0;
	while ((a= scanf("%d", &iq))!=EOF)
	{
		if (iq >= 140)
		{
			printf("Genius\n");
		}
	}
	return 0;
}

或者

#include <stdio.h>

int main()
{
    int n = 0;
    while(scanf("%d", &n) != EOF)
    {
        if(n>=140)
            printf("Genius");
    }
    return 0;
}

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值