basics · beginner · ~15 min

Larger of two

Produce a function the compiler accepts and links.

Challenge

Write a clean, warning-free function the compiler can turn into an object file.

Task

Implement int max2(int a, int b) that returns the larger of the two values (return either when they are equal). No main — the grader calls it.

Input

Two ints a and b.

Output

Returns the larger value, as an int.

Example

max2(3, 7)     ->   7
max2(9, 2)     ->   9
max2(5, 5)     ->   5
max2(-1, -4)   ->   -1

Edge cases

  • Equal inputs return that shared value.
  • Negative values compare normally.

Input format

Two ints a and b.

Output format

The larger of a and b, as an int.

Constraints

a and b are 32-bit signed integers.

Starter code

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

Common mistakes

Failing to handle equality properly or assuming inputs are strictly non-negative.

Edge cases to handle

a == b, negative numbers, INT_MIN/INT_MAX.

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