题目描述
Your task is to Calculate a + b.
输入格式
Input contains multiple test cases. Each test case contains a pair of integers a and b, one pair of integers per line. A test case containing 0 0 terminates the input and this test case is not to be processed.
输出格式
For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.
样例输入
1 5 10 20 0 0
样例输出
6 30
代码解析
-
包含标准输入输出库:
#include <stdio.h>
这一行代码是预处理指令,它告诉编译器在实际编译之前包含标准输入输出库(stdio.h)。这个库提供了进行输入输出操作的功能,比如printf
和scanf
函数。 -
定义主函数:
int main(void)
是C程序的入口点,void
表示这个函数不接受任何参数。 -
定义变量:
int a
和int b
:用于存储用户输入的一对整数。
-
创建无限循环:
while (1)
创建了一个无限循环,条件为1,即始终为真(true)。这意味着,如果没有内部的break
语句,循环将永远执行。 -
读取输入并计算和:
- 在循环内部,首先使用
scanf
函数从标准输入读取一对整数,并存储在变量a
和b
中。 - 然后使用
if
语句检查这对整数是否都是0。如果是,执行break
语句,跳出循环,结束程序。 - 如果这对整数不是都是0,使用
printf
函数输出这对整数的和(a + b
)。
- 在循环内部,首先使用
-
函数返回:
return 0;
表示main
函数执行成功并返回0。在C语言中,main
函数的返回值通常用于表示程序的退出状态,其中0表示成功。
源代码
#include <stdio.h>
int main(void)
{
int a, b;
while (1)
{
scanf("%d%d", &a, &b);
if (a == 0 && b == 0)
break;
printf("%d\n", a + b);
}
return 0;
}