C Program: Sum of Two Integers
A simple C program that reads two integers from the user using scanf(), adds them using the + operator, and displays the result using printf(). It demonstrates basic input/output, variable declaration, and arithmetic operations in C.
#include <stdio.h>
int main() {
int a, b, sum;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum = %d ", sum);
return 0;
}
How It Works?
- #include <stdio.h> — Loads the standard I/O library for printf and scanf.
- int a, b, sum; — Declares three integer variables to hold the two inputs and the result.
- scanf("%d %d", &a, &b); — Reads two integers typed by the user and stores them at the addresses of a and b (the & gives the memory address).
- sum = a + b; — Adds the two values and stores the result in sum.
- printf("Sum = %d ", sum); — Prints the result, where %dis the placeholder for an integer.
- return 0; — Signals successful program termination.
Example Run:
Enter two integers: 7 5
Sum = 12