get到scanf 返回值的知识

while (scanf("%d %d", &a, &b)==2)

printf("%d\n",a+b);

原来还有这样的代码

通过scanf()的返回值来作为判断条件

摘自百度百科

比较一下下面两个,第一个是超出输出限制的(Output Limit Exceeded),然后试了下第一个输入比如一个字母a,那么会无限循环(额,好像是崩了)

第二个的话,只有当正确输入才会有结果,不然就跳出循环了

#include <stdio.h>
int main(void)
{
    int a, b;
    while (1)
    {
    scanf("%d %d", &a, &b);
        printf("%d\n",a+b);
    }return 0;        
}                            
#include <stdio.h>
int main(void)
{
    int a, b;
    while (scanf("%d %d", &a, &b)==2)
        printf("%d\n",a+b);
    return 0;
}                          //这个是原题答案

猜你喜欢

转载自www.cnblogs.com/mo-sheng/p/10574377.html