🚀Beginner

Add Two Numbers

Add two integers entered by user

This program takes two integers as input from the user and displays their sum. It's a fundamental program that teaches variable declaration, user input, arithmetic operations, and formatted output.

📚 What You'll Learn

  • Declaring integer variables with int
  • Reading integers using scanf() with %d
  • Using the address-of operator &
  • Performing arithmetic operations

C Program to Add Two Numbers

add_two_numbers.c
C
1#include <stdio.h>
2
3int main() {
4 // Declare variables to store numbers and result
5 int num1, num2, sum;
6
7 // Get first number from user
8 printf("Enter first number: ");
9 scanf("%d", &num1);
10
11 // Get second number from user
12 printf("Enter second number: ");
13 scanf("%d", &num2);
14
15 // Calculate sum
16 sum = num1 + num2;
17
18 // Display the result
19 printf("%d + %d = %d\n", num1, num2, sum);
20
21 return 0;
22}
Output

Enter first number: 25

Enter second number: 17

25 + 17 = 42

Detailed Code Explanation

Line 4int num1, num2, sum;

Declares three integer variables. Memory is allocated for each variable.

Variable Declaration Syntax:
data_type variable_name;

Multiple variables of same type can be declared in one line, separated by commas.

Line 8scanf("%d", &num1);

Reads an integer from user and stores it in num1.

PartMeaning
%dFormat specifier for integer (decimal)
&num1Address of num1 (where to store the value)

⚠️ Important: The & (address-of) operator is required for scanf() with basic data types like int, float, char. It tells scanf WHERE to store the input.

Line 15sum = num1 + num2;

The + operator adds the values and = stores the result.

Arithmetic Operators in C:
+
Add
-
Subtract
*
Multiply
/
Divide
%
Modulus
Line 18printf("%d + %d = %d\n", num1, num2, sum);

Multiple format specifiers are replaced by corresponding arguments in order:
First %d → num1, Second %d → num2, Third %d → sum

Step-by-Step Execution:

1
Variables Declared

Memory allocated for num1, num2, and sum (each 4 bytes for int)

2
First Prompt

printf displays "Enter first number: "

3
First Input

User types 25, scanf stores it at address of num1

4
Second Prompt

printf displays "Enter second number: "

5
Second Input

User types 17, scanf stores it at address of num2

6
Calculation

sum = 25 + 17 = 42

7
Display Result

printf shows "25 + 17 = 42"

Memory Visualization

num1
25
0x1000
num2
17
0x1004
sum
42
0x1008

Alternative Methods

Without Using Third Variable

main.c
C
1printf("Sum = %d\n", num1 + num2); // Direct calculation in printf

Reading Both Numbers at Once

main.c
C
1printf("Enter two numbers: ");
2scanf("%d %d", &num1, &num2); // Space-separated input

⚠️ Common Mistakes to Avoid

  • Forgetting &: scanf("%d", num1) ❌ crashes! Use scanf("%d", &num1)
  • Wrong format specifier: Using %f for integers
  • Integer overflow: Adding very large numbers exceeds int range

📝 Key Takeaways

int stores whole numbers
%d format specifier for integers
& is required in scanf for variables
+ operator adds two numbers

Want to Learn More?

Explore our comprehensive tutorials for in-depth explanations of C programming concepts.

Browse Tutorials
Back to All Examples