题目描述
Your task is to Calculate a + b.
输入格式
The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.
输出格式
For each pair of input integers a and b you should output the sum of a and b, and followed by a blank line.
样例输入
1 5 10 20
样例输出
6 30
代码解析
-
包含标准输入输出库:
#include <stdio.h>
这一行代码是预处理指令,它告诉编译器在实际编译之前包含标准输入输出库(stdio.h)。这个库提供了进行输入输出操作的功能,比如printf
和scanf
函数。 -
定义主函数:
int main(void)
是C程序的入口点,void
表示这个函数不接受任何参数。 -
定义变量:
int a
和int b
:用于存储用户输入的一对整数。
-
创建循环读取输入:
while (scanf("%d%d", &a, &b) != EOF)
创建了一个循环,条件是scanf
成功读取两个整数到变量a
和b
并且没有到达EOF。EOF(End Of File)是一个特殊的宏,通常表示输入流的结束。在大多数操作系统中,可以通过按下Ctrl+D (Unix/Linux) 或 Ctrl+Z (Windows) 来发送EOF信号。 -
输出结果:
printf("%d\n\n",a+b);
这个函数调用用于输出a
和b
的和。每次成功读取一对整数后,都会执行一次输出。 -
函数返回:
return 0;
表示main
函数执行成功并返回0。在C语言中,main
函数的返回值通常用于表示程序的退出状态,其中0表示成功。
源代码
#include <stdio.h>
int main(void)
{
int a, b;
while (scanf("%d%d", &a, &b) != EOF)
{
printf("%d\n\n",a+b);
}
return 0;
}