ProgrammingHardHCL · Tech Mahindra pattern

What is printed?

What is printed?
#define CLAMP(x, lo, hi) ((x) < (lo) ? (lo) : (x) > (hi) ? (hi) : (x))
int main(void) {
    printf("%d %d %d", CLAMP(5, 1, 10), CLAMP(-2, 1, 10), CLAMP(15, 1, 10));
}
  1. A. 5 -2 15
  2. B. 1 5 10
  3. C. 10 1 5
  4. D. 5 1 10

Correct answer: D. 5 1 10

CLAMP correctly parenthesizes every use of x, lo and hi, so it behaves like a normal clamp function: 5 is within [1,10] so stays 5; -2 is below 1 so clamps to 1; 15 is above 10 so clamps to 10.

More C Programming questions

Exams that ask this topic