basics · beginner · ~15 min

Sum two numbers

Define a function and return a value.

Challenge

Add two integers and return the result. Your first C function.

Task

Implement int sum(int a, int b) that returns a + b. No main — the grader calls it.

Input

Two int arguments a and b, each anywhere in the int range.

Output

The single int a + b.

Example

sum(2, 3)     ->   5
sum(-1, 1)    ->   0
sum(100, 200) ->   300

Edge cases

  • Zero and negative inputs work the same way (sum(-5, -10) is -15).

Why this matters

Reading numbers from stdin and printing a result is the smallest 'real' I/O program. It introduces scanf, return-value checking, and the difference between formatted reads and raw reads.

Input format

Two int arguments a and b.

Output format

The int sum a + b.

Constraints

a and b are valid 32-bit signed integers. Calculation fits in standard int in test assertions.

Starter code

int sum(int a, int b) {
    /* TODO */
    return 0;
}

Common mistakes

Assuming inputs are always positive; attempting to print with printf rather than returning the computed sum.

Edge cases to handle

Zero inputs (0 + 0, 0 + x), negative values, and symmetric additive inverses summing to zero.

Complexity

O(1) — fixed work.

Background lessons

Up next

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.