乘法宏给出错误的答案

#include <iostream>
using namespace std;

#define MULTIPLY(a, b) a*b

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

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

评论
Oswal
Oswal

因为C ++宏不是函数。它们是文本副本,因此意味着:

cout << 2 + 3 * 3 + 5;

那是2 +(3 * 3)+ 5

点赞
评论