#define square(v)v * v; R =正方形(++ p)的; COUT<< R; [英] #define square(v) v*v; r=square(++p); cout<<r;

查看:80
本文介绍了#define square(v)v * v; R =正方形(++ p)的; COUT<< R;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当p为3时,此代码的输出给出25.这是怎么回事。因为输出应该是3 * 3 = 9.



我尝试了什么:



我的输出也是25但是

The output of this code gives 25 when p is 3. How is that happening.because the output should be 3*3=9.

What I have tried:

My output is also coming 25 but how

推荐答案

p 为3时,由于预增量操作,结果永远不会是9。



所以你可能会期待结果是16而不是(++ p = 4)。



但是由于C / C ++宏的一个危险的副作用,你得到了25。当扩展并且参数是语句时多次使用宏参数时,这些语句也会多次执行。



只需手动扩展宏来重现发生的情况:

When p is 3 the result will never be 9 due to the pre increment operation.

So you might expect the result to be 16 instead (++p = 4).

But you got 25 due to one of the dangerous side effects of C/C++ macros. When macro parameters are used multiple times when expanded and the parameters are statements, those statements will be executed also multiple times.

Just expand the macro manually to reproduce what happens:
#define square(v) v*v
r = square(++p);

扩展为(插入空格以便于阅读)

expands to (spaces inserted for readability)

r = ++p * ++p;

该操作的结果未定义(请参阅评估顺序 - cppreference.com [ ^ ])。



为了避免这种情况,请不要使用宏。改为使用(内联)函数:

The result of that operation is undefined (see Order of evaluation - cppreference.com[^]).

To avoid such don't use macros. Use (inline) functions instead:

int square(int v) { return v * v; }


#defines是一个文本替换工具,它们不像子程序,但展开以提供代码。

所以当你这样写:

#defines are a text replacement facility, they don't act like subroutines, but "expand" out to provide the code.
So when you write this:
#define square(v) v*v;

r=square(++p); 
cout<<r;

编译器看到的是:

What the compiler sees is this:

r=++p * ++p; 
cout<<r;

这意味着结果不会是可预测的,它会根据您使用的编译器而有所不同。在这种情况下,它导致 5 * 5 ,但如果找到合适的编译器,它也可以生成16,12或9!

请参阅此处:为什么x = ++ x + x ++给我错误的答案? [ ^ ]

And that means that the result is not going to be that predictable, it will vary depending on which compiler you use. In this case, it's resulting in 5 * 5, but it could also generate 16, 12, or 9 if you find the right compiler!
See here: Why does x = ++x + x++ give me the wrong answer?[^]


此代码:

This code :
#define square(v) v*v;
r= square(++p);
cout<<r;



预处理为:


is preprocessed as:

#define square(v) v*v;
r= ++p * ++p;
cout<<r;



带您进入无法预测的代码。

根据编译器的行为,代码可以编译为:


which bring you in the happy land of unpredictable code.
Depending on compiler behavior, the code can be compiled as:

#define square(v) v*v;
++p;
++p;
r= p * p; // R= 5*5= 25
cout<<r;



或as:


or as:

#define square(v) v*v;
++p;
r= p;
r= r * ++p; // r= 4*5= 20
cout<<r;



从编译器的角度来看,这两个结果都是合法的(和语言定义)。




Both result are legal from compiler point of view (and language definition).

#define square(v) v*v;



定义是一个错误的定义,可以提供均匀的更有趣的是:


The define is a bad define that can give even more fun with:

#define square(v) v*v;
p=3;
r= square(p+1);
cout<<r;



它被预处理为:


it is preprocessed as:

#define square(v) v*v;
p=3;
r= p+1*p+1;
cout<<r;



这意味着:


which means:

#define square(v) v*v;
p=3;
r= p +(1 * p) +1; // r= 3+3+1= 7
cout<<r;



正确的定义是:


Correct define would be:

#define square(v) (v)*(v);


这篇关于#define square(v)v * v; R =正方形(++ p)的; COUT&LT;&LT; R;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆