#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main(void)
{
long num;
long sum = 0l;
_Bool is_input_good;
printf("请输入一个整数(q退出):");
is_input_good = (1 == scanf("%ld", &num));
while (is_input_good)
{
sum += num;
printf("请输入一个整数(q退出):");
is_input_good = (1 == scanf("%ld", &num));
}
printf("所有输入整数和是:%ld\n", sum);
return 0;
}
输出:
请输入一个整数(q退出):1
请输入一个整数(q退出):2
请输入一个整数(q退出):3
请输入一个整数(q退出):q
所有输入整数和是:6
scanf() 函数出错返回 0,遇文件结束返回 EOF, 其返回值是输入成功的元素个数,上例中成功返回 1。
c99 中提供了stdbool.h
头文件, 让bool
成为_Bool
的别名, 这样可以和 c++ 兼容。
参考:
c primer plus 第6版 p54