Preincrement运算符 [英] Preincrement operator

查看:112
本文介绍了Preincrement运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人请解释一下:

int a = 3,b;

b = ++ a +(++ a);


当期望值为b = 9时,b = 10?

Someone Please explain this:
int a=3,b;
b=++a+(++a);

This gives b=10 when expected value is b=9?

推荐答案


有人请解释一下:

int a = 3,b;

b = ++ a +(++ a);


这给出b = 10当期望值是b = 9?
Someone Please explain this:
int a=3,b;
b=++a+(++a);

This gives b=10 when expected value is b=9?





在声明中b = ++ a +(++ a),a在执行此语句之前递增两次。作为这种a的结果。其中包含值5。


现在必须执行的操作是b = a + a,b = 5 + 5。因此结果。


问候,

Pradeep

Hi,
In the statement b=++a+(++a), "a" is incremented twice before the execution of this statement. As a result of this "a" contains value 5 in it.

Now the operation that has to be performed is b=a+a i.e, b=5+5. Hence the result.

Regards,
Pradeep



有人请解释一下:

int a = 3,b;

b = ++ a +(++ a);


当期望值为b = 9时,这给出b = 10?
Someone Please explain this:
int a=3,b;
b=++a+(++a);

This gives b=10 when expected value is b=9?



结果未定义。原因是,在达到序列点之前,左值可能不会被修改为更多

。如果没有遵守这个''规则'

未定义的行为是结果。


亲切的问候,


Jos

The result is undefined. The reason is that an lvalue may not be modified more
than once before a sequence point has been reached. If this ''rule'' isn''t obeyed
undefined behaviour is the result.

kind regards,

Jos



结果未定义。原因是,在达到序列点之前,左值可能不会被修改为更多

。如果没有遵守这个''规则'

未定义的行为是结果。


亲切的问候,


Jos
The result is undefined. The reason is that an lvalue may not be modified more
than once before a sequence point has been reached. If this ''rule'' isn''t obeyed
undefined behaviour is the result.

kind regards,

Jos



Jos是对的,


Adrian

Jos is right,


Adrian


这篇关于Preincrement运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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