basics · beginner · ~15 min

Add two integers

Write and return from your first C function.

Challenge

Write your first C function: take two integers and return their sum.

Task

Implement int add(int a, int b) that returns a + b. No main — the grader calls your function.

Input

Two ints a and b.

Output

Returns their sum, as an int.

Example

add(2, 3)    ->   5
add(-4, 1)   ->   -3
add(0, 0)    ->   0

Edge cases

  • Negative operands work the same way.

Input format

Two ints a and b.

Output format

Their sum, as an int.

Constraints

a and b are signed 32-bit integers whose sum fits in a standard int.

Starter code

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

Common mistakes

Printing the result with printf rather than returning it from the function.

Edge cases to handle

Zeros, negative operands, additive inverses yielding zero.

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