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));
}- A. 5 -2 15
- B. 1 5 10
- C. 10 1 5
- 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
- What is printed? Assume int is 32-bit, two's complement. c int x = INTMAX; int y = x + 1; printf("%d", y);
- What is the behavior? c int p = malloc(10 sizeof(int)); free(p); printf("%d", p[0]);
- What is printed? c int i = 1; int arr[5] = {0}; int x = i + i++; printf("%d", x);
- What is the behavior? c int arr[5] = {1,2,3,4,5}; int p = arr + 5; printf("%p", (void)p); printf("%d", p);
- What is the behavior? c char buf[5]; strcpy(buf, "hello world"); printf("%s", buf);
- What is the behavior? c int a = 10; int b = a << 32; printf("%d", b);