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
  • 349,597
  • 67
  • 533
  • 578

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
  • 349,597
  • 67
  • 533
  • 578
STF_ZBR
  • 617
  • 1
  • 4
  • 19