Adding two numbers might sound like the easiest thing in programming and honestly, it kind of is. But it’s also the perfect starting point if you’re just getting into C language. Let’s walk through different ways to write a program that adds two numbers in C. By the end, you’ll not only know how to write the code but also why it works.

Copy Code
#include <stdio.h> // Including standard input-output header
int main() {
int num1, num2, sum; // Declaring variables
// Asking the user to input two numbers
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
// Calculating the sum
sum = num1 + num2;
// Displaying the result
printf("Sum: %d", sum);
return 0;
}Output:
Enter two integers: 7 5
Sum: 12
Explanation:
This basic program asks the user to enter two integers using scanf(), stores them in num1 and num2, then adds them using the + operator and stores the result in sum. Finally, it prints the result using printf().
Copy Code
#include <stdio.h>
int main() {
float num1, num2, sum;
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
sum = num1 + num2;
printf("Sum: %.2f", sum);
return 0;
}Output:
Enter two numbers: 3.7 6.2
Sum: 9.90
Explanation:
Here we’re using float instead of int, so the program can handle decimal numbers. The format specifier %.2f ensures the result is displayed with two decimal places handy for cleaner outputs.
Copy Code
#include <stdio.h>
// Function to add two integers
int add(int a, int b) {
return a + b;
}
int main() {
int num1, num2, sum;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
sum = add(num1, num2); // Calling the function
printf("Sum: %d", sum);
return 0;
}Output:
Enter two integers: 9 4
Sum: 13
Explanation:
Breaking things into functions makes your code modular and clean. The add() function takes two integers as arguments and returns their sum. This way, you can reuse the logic elsewhere too.
Copy Code
#include <stdio.h>
int main() {
int num1, num2, sum;
int *p1, *p2;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
p1 = &num1;
p2 = &num2;
sum = *p1 + *p2;
printf("Sum: %d", sum);
return 0;
}Output:
Enter two integers: 11 13
Sum: 24
Explanation:
This method introduces pointers yes, they sound scary at first, but they’re just variables that store memory addresses. We point p1 and p2 to num1 and num2, and then access the actual values using the * operator (dereferencing).
Copy Code
#include <stdio.h>
int main() {
int nums[2], sum;
printf("Enter two integers: ");
scanf("%d %d", &nums[0], &nums[1]);
sum = nums[0] + nums[1];
printf("Sum: %d", sum);
return 0;
}Output:
Enter two integers: 6 3
Sum: 9
Explanation:
This time, instead of declaring two separate variables, we’re using an array to store both numbers. Arrays help when you’re dealing with lots of values but for just two, it’s mostly a learning exercise.
Q1. Why should I learn how to add two numbers in C?
Because it’s your first step into programming logic! It teaches you variable declaration, input/output handling, and basic arithmetic in C.
Q2. What's the difference between int and float in C?
int is for whole numbers like 3, 10, or -5. float is for numbers with decimals like 3.14 or -0.5.
Q3. When should I use functions for simple operations like addition?
Whenever you want reusable, modular code. Even small tasks benefit from being separated logically—it helps with readability and maintenance.
Q4. Is it necessary to use pointers for simple addition?
Not really, but it helps you get comfortable with one of C's core concepts. Pointers are essential for advanced topics like memory management and dynamic arrays.
Q5. Can I use cin and cout instead of scanf and printf?
Not in C. cin and cout belong to C++ C uses scanf and printf for input and output.
If you're just getting started with C, playing around with small programs like this one is the best way to build a strong foundation. Try modifying the code, use different data types, and make small errors (yes, that’s a good thing!) you’ll learn much faster.
If you're serious about mastering C programming and building a strong foundation in coding, check out Uncodemy’s C Programming Course.
Personalized learning paths with interactive materials and progress tracking for optimal learning experience.
Explore LMSCreate professional, ATS-optimized resumes tailored for tech roles with intelligent suggestions.
Build ResumeDetailed analysis of how your resume performs in Applicant Tracking Systems with actionable insights.
Check ResumeAI analyzes your code for efficiency, best practices, and bugs with instant feedback.
Try Code ReviewPractice coding in 20+ languages with our cloud-based compiler that works on any device.
Start Coding
TRENDING
BESTSELLER
BESTSELLER
TRENDING
HOT
BESTSELLER
HOT
BESTSELLER
BESTSELLER
HOT
POPULAR