C++ 乘法宏给出了错误的答案
我以为这是40,因为5乘8等于40。它为什么打印16?< /p> ,因为C++宏不是函数。它们是文本副本,因此这意味着:C++ 乘法宏给出了错误的答案,c++,macros,c-preprocessor,C++,Macros,C Preprocessor,我以为这是40,因为5乘8等于40。它为什么打印16?< /p> ,因为C++宏不是函数。它们是文本副本,因此这意味着: #include <iostream> using namespace std; #define MULTIPLY(a, b) a*b int main(){ cout << MULTIPLY(2+3, 3+5); return 0; } 是2+3×3+5</p>< p>,因为C++宏不是函数。它们是文本副本,因此这意味着: #i
#include <iostream>
using namespace std;
#define MULTIPLY(a, b) a*b
int main(){
cout << MULTIPLY(2+3, 3+5);
return 0;
}
是2+3×3+5</p>< p>,因为C++宏不是函数。它们是文本副本,因此这意味着:
#include <iostream>
using namespace std;
#define MULTIPLY(a, b) a*b
int main(){
cout << MULTIPLY(2+3, 3+5);
return 0;
}
也就是2+3*3+52+3*3+5不是40。试着用这个来定义MULTIPLYa,ba*b,然后想想区别是什么。但是比任何宏int MULTIPLYint a,int b{返回a*b;}更好。宏是魔鬼™.不要使用宏。一个很好的说明为什么宏是坏的:MULTIPLY4,Hello;如果MULTIPLY是一个函数,编译器将捕获它。2+3*3+5不是40。请尝试定义MULTIPLYa,b a*b,并思考区别是什么。但甚至比任何宏int MULTIPLYint a,int b{返回a*b;}更好。宏是魔鬼™.不要使用宏。一个很好的说明为什么宏是坏的:MULTIPLY4,Hello;如果乘法是一个函数,编译器将捕获它。