ProgrammingModerateAccenture · Capgemini

What is printed?

What is printed?
int up(int a, int b) { return (a + b - 1) / b; }
int main(void) { printf("%d %d %d", up(10, 3), up(9, 3), up(0, 3)); }
  1. A. 4 4 1
  2. B. 3 3 0
  3. C. 4 3 1
  4. D. 4 3 0

Correct answer: D. 4 3 0

(a + b − 1) / b is the integer ceiling of a / b for non-negative a and positive b: 12 / 3 = 4, 11 / 3 = 3, and 2 / 3 = 0. Plain a / b would give 3 for the first. '4 4 1' imagines it always rounds up by one.

More C Programming questions

Exams that ask this topic