+ 1

Explain why the output is 12 instead of 9

#include <iostream> using namespace std; #define sq(x) (x)*(x) int main() { int a=3; cout<<sq(a++); return 0; }

18th Aug 2019, 7:50 AM
Tom
Tom - avatar
1 Answer
0
You increment a by one with the '++' term after the first operation, so it is 3 times 4 = 12. Delete ++ to calculate the square of 3.
18th Aug 2019, 7:55 AM
Tom Hammerbacher
Tom Hammerbacher - avatar