Interview Prep
Interview questions, roadmaps and practice sets
Tutorials
Free tutorials on 30+ topics
Blog
Career guides, skills and interview tips
Free PDFs
Notes, cheat sheets and question banks
Certificates
Free skill tests and course certificates
Tools & Whiteboard
Free tools you can use right in the browser
Practice Tests
Mock tests, MCQs and quick Q&A
Free LMS
Learn on the Uncodemy LMS
Choose the best training program according to your career.
Improve the performance of your business with our innovative Industrial Training Program.
Read More →
Our instructor-led training classes are designed to make you an expert.
Read More →
Real-life case studies are a great way to demonstrate your practical knowledge.
Read More →
Explore blogs, latest technologies and industry updates with practical examples.
Read More →Browse all available training programs and choose the right one for your career.
Read More →Know more about who we are and what we stand for.
Discover our mission to equip individuals with essential IT skills and industry expertise.
Read More →
Free tutorials, interview questions, tools and calculators to help you learn.
Explore →Learn, practice and plan your career — bilkul free.
Book a free video counselling session and pick the right course for you.
Read More →Adding two numbers is usually the second C program anyone writes, right after “Hello, World!”. It looks tiny, but it teaches variables, data types, printf, scanf, format specifiers and functions — the building blocks of everything that comes later.

Below are 7 versions, from the simplest to some interview-style variations. Every program was compiled with gcc -Wall -Wextra and run.
#include <stdio.h>
int main(void) {
int a = 25, b = 17;
int sum = a + b;
printf("Sum of %d and %d is %d\n", a, b, sum);
return 0;
}
Output
Sum of 25 and 17 is 42
#include <stdio.h> gives us printf and scanf.int stores whole numbers.%d in printf is replaced by the integer values in order.#include <stdio.h>
int main(void) {
int a, b;
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
printf("Sum = %d\n", a + b);
return 0;
}
Output
Enter first number: 12
Enter second number: 30
Sum = 42
The & in &a gives scanf the address of a, so it can store the value there. Forgetting & is the most common error in this program.
#include <stdio.h>
int add(int x, int y) {
return x + y;
}
int main(void) {
printf("add(8, 12) = %d\n", add(8, 12));
printf("add(-5, 3) = %d\n", add(-5, 3));
return 0;
}
Output
add(8, 12) = 20
add(-5, 3) = -2
Functions make code reusable. Lab exams often ask for this version.
#include <stdio.h>
int main(void) {
float a, b;
printf("Enter two decimal numbers: ");
scanf("%f %f", &a, &b);
printf("Sum = %.2f\n", a + b);
return 0;
}
Output
Enter two decimal numbers: 2.5 3.75
Sum = 6.25
Use %f for float (and %lf in scanf for double). %.2f prints exactly two digits after the decimal point.
#include <stdio.h>
void add(int *x, int *y, int *result) {
*result = *x + *y;
}
int main(void) {
int a = 40, b = 2, sum;
add(&a, &b, &sum);
printf("Sum = %d\n", sum);
return 0;
}
Output
Sum = 42
The function receives addresses and writes the answer into sum through a pointer. This is how C functions “return” more than one value.
What if the user types “ten” instead of 10? scanf returns the number of values it successfully read, so we can check it.
#include <stdio.h>
int main(void) {
int a, b;
printf("Enter two integers: ");
if (scanf("%d %d", &a, &b) != 2) {
printf("Invalid input. Please enter numbers only.\n");
return 1;
}
printf("Sum = %d\n", a + b);
return 0;
}
Output (wrong input)
Enter two integers: ten 5
Invalid input. Please enter numbers only.
Output (correct input)
Enter two integers: 7 8
Sum = 15
An int is usually 32 bits, so its maximum value is 2,147,483,647 (INT_MAX). Adding 1 to it overflows — in C this is undefined behaviour for signed integers. Use a bigger type like long long:
#include <stdio.h>
#include <limits.h>
int main(void) {
int a = INT_MAX, b = 1;
long long big = (long long)a + b;
printf("INT_MAX = %d\n", INT_MAX);
printf("Using long long: %lld\n", big);
return 0;
}
Output
INT_MAX = 2147483647
Using long long: 2147483648
The cast (long long)a makes C do the addition in the bigger type before the overflow can happen.
A popular interview question. It uses bitwise operators: XOR (^) adds bits without carry, and AND (&) shifted left finds the carry.
#include <stdio.h>
int add_without_plus(int a, int b) {
while (b != 0) {
unsigned carry = (unsigned)(a & b) << 1;
a = a ^ b;
b = (int)carry;
}
return a;
}
int main(void) {
printf("%d\n", add_without_plus(15, 27));
printf("%d\n", add_without_plus(-4, 10));
return 0;
}
Output
42
6
The carry is calculated as unsigned so that shifting a negative value doesn’t cause undefined behaviour.
| Type | printf | scanf |
|---|---|---|
| int | %d |
%d |
| float | %f |
%f |
| double | %f (or %lf) |
%lf |
| long long | %lld |
%lld |
| char | %c |
%c |
Imagine someone writing program 2 for the first time. It compiles, they type 12 and 30, and the answer is some random number like 21845 — or the program crashes. The code “looks” right. The missing piece is a single character: scanf("%d", a) instead of scanf("%d", &a). Without &, scanf treats the garbage value inside a as an address and writes there. Turning on warnings (gcc -Wall) would have pointed straight at that line — which is why it’s worth always compiling with warnings on.
& in scanf.%d to print a float (or %f for an int) — the output will be wrong.void main() — use int main(void) and return 0;.\n at the end of printf, so the next prompt appears on the same line.Write int add(int x, int y) { return x + y; } and call it from main() (see program 3).
scanf needs the memory address of the variable so it can store the value there.
Use long long (up to about 9.2 × 10^18). For even bigger numbers, store digits in an array or string and add digit by digit.
scanf and its return value: https://en.cppreference.com/w/c/io/fscanfINT_MAX): https://en.cppreference.com/w/c/types/limits