Back to questions
JuniorTheoryCommon
What does the program print?
#include <iostream>
#define SQUARE(x) x * x
int main() {
int x = 3;
std::cout << SQUARE(x + 1) << '\n';
}7
- Expecting
16: a macro pastes its argument as text, soSQUARE(x + 1)becomesx + 1 * x + 1, not(x + 1) * (x + 1). - Expecting
13: after the substitution the usual precedence applies, so1 * xis computed first and3 + 3 + 1is7.
- How would you change
SQUAREso thatSQUARE(x + 1)gives16? - Why is
SQUARE(++x)a problem even when the macro is fully parenthesized?