Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 乘法宏给出了错误的答案_C++_Macros_C Preprocessor - Fatal编程技术网

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

我以为这是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++宏不是函数。它们是文本副本,因此这意味着:

#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

2+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;如果乘法是一个函数,编译器将捕获它。