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);
}
  1. A. 11
  2. B. 10
  3. C. Compile-time error: macros cannot span multiple lines
  4. 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

Exams that ask this topic