1
#include <iostream>
using namespace std;

#define MULTIPLY(a, b) a*b

int main(){
    cout << MULTIPLY(2+3, 3+5);
    return 0;
}

I expected this to print 40 since five times eight is forty. Why does it print 16?

John Kugelman
  • 330,190
  • 66
  • 504
  • 555

1 Answers1

5

Because C++ macros are not functions. They are text copies, so that means:

cout << 2+3*3+5;

Which is 2 + (3*3) + 5

John Kugelman
  • 330,190
  • 66
  • 504
  • 555
STF_ZBR
  • 477
  • 3
  • 15