#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →

Sum of Two Numbers in C with Input and Output Example

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.

Top-PCM-Career-Options-after-12th-Grade

Sum of Two Numbers in C: 7 Programs with Input and Output

Below are 7 versions, from the simplest to some interview-style variations. Every program was compiled with gcc -Wall -Wextra and run.

1. Sum of two fixed numbers

#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.

2. Taking input from the user (scanf)

#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.

3. Using a function

#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.

4. Adding decimal numbers (float)

#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.

5. Using pointers

#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.

6. With input validation

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

7. When the sum is too big for int

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.

Bonus: add two numbers without the + operator

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.

Format specifiers you need

Type printf scanf
int %d %d
float %f %f
double %f (or %lf) %lf
long long %lld %lld
char %c %c

A common beginner scenario

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.

Common mistakes

  • Forgetting & in scanf.
  • Using %d to print a float (or %f for an int) — the output will be wrong.
  • Writing void main() — use int main(void) and return 0;.
  • Ignoring overflow for large inputs.
  • Missing \n at the end of printf, so the next prompt appears on the same line.

Frequently asked questions

How do I add two numbers in C using a function?

Write int add(int x, int y) { return x + y; } and call it from main() (see program 3).

Why is & used in scanf?

scanf needs the memory address of the variable so it can store the value there.

How can I add very large numbers in C?

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.

References

Placed Students

Our Clients

Partners