basics · beginner · ~15 min
Define a function and return a value.
Implement int sum(int a, int b) that returns a + b. Do not write a main — the grader supplies one.
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.
int sum(int a, int b) {
/* TODO */
return 0;
}
Forgetting & on the scanf argument (passes the value, not the address — silently undefined). Not checking scanf's return (a non-numeric input silently leaves a and b uninitialised). Printing on the wrong line.
Negative numbers (-3 5). Very large numbers that overflow int. Inputs separated by tab or newline (scanf treats all whitespace the same).
O(1) — fixed work.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.