乘法宏给出错误的答案[重复]

问题描述 投票:0回答:1
#include <iostream>
using namespace std;

#define MULTIPLY(a, b) a*b

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

我希望这会打印40,因为五乘八等于四十。为什么打印16

c++ macros c-preprocessor
1个回答
4
投票

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

cout << 2+3*3+5;

哪个是2 +(3 * 3)+ 5

© www.soinside.com 2019 - 2024. All rights reserved.