C Program: Read Two Integers and Print their SUM With Example

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?

  1. #include <stdio.h> — Loads the standard I/O library for printf and scanf.
  2. int a, b, sum; — Declares three integer variables to hold the two inputs and the result.
  3. 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).
  4. sum = a + b; — Adds the two values and stores the result in sum.
  5. printf("Sum = %d ", sum); — Prints the result, where %dis the placeholder for an integer.
  6. return 0; — Signals successful program termination.

Example Run:

Enter two integers: 7 5
Sum = 12

3 Views