ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define CLAMP(x, lo, hi) MIN(MAX(x, lo), hi)
#define MAX(a, b) ((a) > (b) ? (a) : (b))
int main(void) {
printf("%d", CLAMP(15, 0, 10));
}- A. 15
- B. 0
- C. 10
- D. Compile-time error: MAX is used in CLAMP before being defined
Correct answer: C. 10
Macros are expanded only at the point of USE (in the translation unit, after the preprocessor has seen the whole file's relevant #define directives before compilation proper begins), not strictly in textual declaration order for nested macro calls within a single file, as long as MAX is defined somewhere before CLAMP is actually invoked in the code being compiled. CLAMP(15,0,10) computes MAX(15,0)=15, then MIN(15,10)=10.
More C Programming questions
- What is printed? c #define INCR(x) (++x) int arr[3] = {10, 20, 30}; int i = 0; printf("%d", arr[INCR(i)]);
- What is printed? c #define ISEVEN(n) (((n) % 2) == 0) int main(void) { int x = 7; if (ISEVEN(x)) printf("even"); else printf("odd"); }
- What happens when this runs? c int x; printf("%d", x);
- What happens when this runs? c char p; { char buf[20] = "hello"; p = buf; } printf("%s", p);
- What happens when this runs? c int a[5] = {1, 2, 3, 4, 5}; a[5] = 100; printf("%d", a[5]);
- What happens when this runs? c int p = NULL; printf("%d", p);