ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
#define MULTI_LINE(x) \
do { \
x = x * 2; \
x = x + 1; \
} while (0)
int main(void) {
int v = 5;
MULTI_LINE(v);
printf("%d", v);
}- A. 11
- B. 10
- C. Compile-time error: macros cannot span multiple lines
- D. 6
Correct answer: A. 11
The backslash-newline sequences join the multi-line macro body into one logical line for the preprocessor; the do { ... } while(0) wrapper is the standard idiom to make a multi-statement macro behave syntactically like a single statement (safe even inside an if without braces). v=5 becomes 5*2=10, then 10+1=11.
More C Programming questions
- What is printed? c #define DEBUGPRINT(fmt, ...) printf(fmt, VAARGS) int main(void) { DEBUGPRINT("%d and %dn", 1, 2); }
- What is printed? c #define SAFEDIV(a, b) ((b) != 0 ? (a) / (b) : 0) int main(void) { int x = 10, y = 0; printf("%d", SAFEDIV(x, y)); }
- What is printed? c #if 1 #define VALUE 100 #elif 0 #define VALUE 200 #else #define VALUE 300 #endif printf("%d", VALUE);
- What is printed? c #define ISPOSITIVE(x) ((x) > 0) #define ISNEGATIVE(x) ((x) < 0) #define ISZERO(x) ((x) == 0) int classify(int n) { if…
- Assume a 64-bit LP64 ABI. What is printed? c int findMax(int arr, int n) { int maxPtr = arr; for (int i = 1; i < n; i++) if (arr[i] >…
- Assume a 64-bit LP64 ABI. What is printed? c int binarySearch(int arr, int n, int target) { int lo = 0, hi = n - 1; while (lo <= hi) { int…