✓ Link copied to clipboard
C 📅 July 2, 2026 ⏱ 5 min read

C Program to Add Two Integers with Explanation and Output

C Program to Add Two Integers with Explanation and Output

Adding two numbers is one of the first things you’ll tackle in C programming. Let’s check out how to add two integers in C. Picture this: you've got 5 and 3. The program gives you their sum, which is 8.

This article shows you how to whip up a simple C program to add two integers, and it'll dive into each step involved.

Prerequisites for the C Program to Add Two Integers

Before we start, make sure you're familiar with these:

  • Basic understanding of C programming syntax
  • The int data type to store integer values
  • Standard input and output functions like scanf and printf
  • Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler

Source Code of C Program to Add Two Integers

Here's the complete source code for adding two integers in C:

add_two_integers.cC▶ Run
#include <stdio.h>

int main() {
    int a, b, sum;

    // Input two integers from the user
    printf("Enter the first integer: ");
    scanf("%d", &a);
    printf("Enter the second integer: ");
    scanf("%d", &b);

    // Calculate the sum
    sum = a + b;

    // Print the sum
    printf("Sum = %d\n", sum);

    return 0;
}

Try this code yourself — free

Run it in the EduSeekho online compiler. No setup needed.

▶ Open Compiler

Output of the Add Two Integers Program

When you run this program, it will ask for two integers and display their sum. Here's a sample execution:

Output
Enter the first integer: 5
Enter the second integer: 3
Sum = 8
Output of C Program to Add Two Integers
Output of C Program to Add Two Integers

Algorithm to Add Two Integers in C

Adding two integers involves reading them from the user, calculating their sum, and then displaying the result. Here is the step-by-step logic:

  1. Read the first integer a from the user.
  2. Read the second integer b from the user.
  3. Calculate the sum of a and b.
  4. Display the result of the addition.
flowchart TD
  A([Start]) --> B[/Read first integer a/]
  B --> C[/Read second integer b/]
  C --> D[sum = a + b]
  D --> E[/Print sum/]
  E --> F([End])
Flowchart: the algorithm step by step

How the C Program to Add Two Integers Works

Let's break down the code line by line to see how it works.

  • The line #include <stdio.h> includes the Standard Input Output library needed for functions like printf and scanf.
  • The int main() function is where the program execution starts.
  • We declare integer variables a, b, and sum to store the user inputs and the result.
  • printf is used to prompt the user to enter the first integer.
  • scanf("%d", &a) reads the integer input from the user and stores it in a.
  • The process is repeated for the second integer b.
  • The statement sum = a + b performs the addition of a and b.
  • printf("Sum = %d\n", sum) prints the result on the screen.
  • The program ends with return 0 indicating successful execution.
🧠

Quick Quiz — test your understanding

Tap an option to check your answer

What does the 'sum = a + b' line do in this program?

🎉Correct! This line calculates the sum of a and b and stores it in the variable sum.
🤔Not quite. Think about what operation is being performed and what follows.

Time and Space Complexity of the C Program to Add Two Integers

This program efficiently handles the addition of two numbers with minimal resources.

Time Complexity: O(1). The program performs a constant amount of work independent of the input, as it only does a single addition operation.

Space Complexity: O(1). It uses only three integer variables: a, b, and sum.

⚠️

Common mistake: Not including the <stdio.h> header can cause errors when using printf and scanf.

Key takeaway: This program's a simple intro to arithmetic in C, showing how user input and basic calculations work together.

FAQs About C Program to Add Two Integers

Here are some common questions beginners have about this program:

The int data type's used to hold integer values here.
In C, scanf gets the input, while cin is what you see in C++. Since this program's in C, scanf is the way to go.
The program's looking for integers and can act weird if you throw in non-integer values. This might mess up the output or even crash it.
Yep, the size of integers depends on your system's architecture, usually between -2,147,483,648 and 2,147,483,647 in a 32-bit system.
You can definitely add more variables and repeat the input and addition for extra numbers. Loops are great for making it more flexible.

If you found this program helpful, check out these related programs:

Conclusion

When you’re dealing with user input, think about input validation — it'll make sure the entries are integers. It’ll help avoid unexpected behavior. Try tweaking this code to work with more numbers or even floating-point values. These skills will come in handy as you dive into more complex stuff.

Get more practice with C programming through our C programming quiz and try out new programs in the online compiler.