ProgrammingHardIBM · Deloitte pattern
What is printed?
What is printed?
#define SQUARE(x) x * x
int main() {
int a = 5;
printf("%d", SQUARE(a + 1));
}- A. 6
- B. 11
- C. 36
- D. Compilation error: macro arguments must be parenthesized
Correct answer: B. 11
SQUARE(a+1) expands to the raw text a + 1 * a + 1, which by operator precedence (* before +) is a + (1*a) + 1 = 5 + 5 + 1 = 11, nowhere near the intended 6*6=36. Proper macro hygiene requires `#define SQUARE(x) ((x)*(x))`.
More C Programming questions
- What is printed? c #define INCR(x) x++ int main() { int i = 5; int r = 2 INCR(i); printf("%d %d", r, i); }
- What is printed? c #define PR(x) printf(#x " = %dn", x) int main() { int value = 42; PR(value); }
- What is printed? c #define CONCAT(a, b) a##b int main() { int xy = 100; printf("%d", CONCAT(x, y)); }
- What is printed? c #define ISEVEN(n) ((n) % 2 == 0) int main() { int x = 7; if (!ISEVEN(x)) printf("odd"); else printf("even"); }
- Assume 32-bit int. What is printed? c int flags = 0; flags |= (1 << 2); flags |= (1 << 4); flags &= ~(1 << 2); printf("%d", flags);
- What is printed? c #if defined(DEBUG) && !defined(RELEASE) #define LOG(x) printf("[debug] %sn", x) #else #define LOG(x) #endif int main()…