关于+ =和++ [英] About += and ++

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

问题描述

我是C的新手并且怀疑+ =和++运算符。写作

''i + = 1''对''我'造成的影响与写''++ i'(或''i ++'',但
$相同b $ b获取的值将不同)。但是因为''i + = 1''相当于''i = i + 1''相当于
,所以它是一个重新分配,对吗?那么,

写''++ i'还是''i ++''也是一个重新分配?在这种情况下,编译器在内部执行什么
来增加值?或者是

实现定义的行为?


谢谢,

塞巴斯蒂安

I''m new to C and having doubt about the += and ++ operators. Writing
''i += 1'' causes the same effect on ''i'' as writing ''++i'' (or ''i++'', but
the fetched value will be different). But since ''i += 1'' is rather
equivalent to ''i = i + 1'', then it is a reassignment, right? So, is
writing ''++i'' or ''i++'' also a reassignment? What is the compiler doing
internally to increment the value in that case? Or is that
implementation-defined behavior?

Thanks,
Sebastian

推荐答案

在文章< 48 ********************************** @ d45g2000hsc。 googlegroups.com>,

< s0 **** @ gmail.comwrote:
In article <48**********************************@d45g2000hsc. googlegroups.com>,
<s0****@gmail.comwrote:

>我是C的新手并且怀疑+ =和++运算符。写作''i + = 1''对''i''造成的影响与写''++ i''(或''i ++'')相同,但是取得的值将是不同)。但是因为''i + = 1''相当于''i = i + 1'',所以它是一个重新分配,对吗?那么,写''++ i'还是''i ++''也是一个重新分配?
>I''m new to C and having doubt about the += and ++ operators. Writing
''i += 1'' causes the same effect on ''i'' as writing ''++i'' (or ''i++'', but
the fetched value will be different). But since ''i += 1'' is rather
equivalent to ''i = i + 1'', then it is a reassignment, right? So, is
writing ''++i'' or ''i++'' also a reassignment?



是的,所有这些都为i赋值。作为陈述,i + = 1,i ++,

和++ i,都等于i = i + 1,编译器可以(和

可能会)实现它们完全相同。


正如你所说,i ++在另一个表达中使用时是不同的。


- Richard

-

在紧接着数字9的两个字符的选择中,

应考虑将图形10和11更换为

促进采用纯正货币区域的代码。 (X3.4-1963)

Yes, all of them assign new values to i. As statements, i+=1, i++,
and ++i, are all equivalent to i=i+1, and the compiler can (and
probably will) implement them all identically.

As you note, i++ is different when used within another expression.

-- Richard
--
In the selection of the two characters immediately succeeding the numeral 9,
consideration shall be given to their replacement by the graphics 10 and 11 to
facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)


s0 **** @ gmail.com 写道:
s0****@gmail.com wrote:

我是C的新手并且怀疑+ =和++运算符。写作

''i + = 1''对''我'造成的影响与写''++ i'(或''i ++'',但
$相同b $ b获取的值将不同)。
I''m new to C and having doubt about the += and ++ operators. Writing
''i += 1'' causes the same effect on ''i'' as writing ''++i'' (or ''i++'', but
the fetched value will be different).



是。

Yes.


但是因为''i + = 1''相当于'' i = i + 1'',然后它

是一个重新分配,对吗?
But since ''i += 1'' is rather equivalent to ''i = i + 1'', then it
is a reassignment, right?



嗯,这是一项任务;没有什么特别的,除非你

意味着特别的东西。你呢?

Well, it''s an assignment; nothing special about it, unless you
mean something special. Do you?


那么,写''++ i'还是''i ++''也是一个重新分配?
So, is writing ''++i'' or ''i++'' also a reassignment?



更新`i` - 它为它赋予一个新值。如果有资格将

作为重新分配,是的。

It updates `i` -- it assigns it a new value. If that qualifies
as a reassignment, yes.


编译器在内部做什么来增加值

在那种情况下?
What is the compiler doing internally to increment the value
in that case?



它正在生成增加变量的代码。

It''s generating code to increment the variable.


或者是实现定义的行为?
Or is that implementation-defined behavior?



否;它是特定于实现的。 (实现定义的是C标准的技术术语,意思是实施必须

做一些关于X /的事情并告诉你它做了什么/ 。实施 -

具体不是标准的术语;我的意思是

实现做它做的任何事情,并没有义务告诉你

那是什么。)


有些机器(例如PDP-11)有花哨的增量。说明

将1添加到寄存器(甚至是内存位置)。

其他机器必须使用普通的添加按钮。指令用

一个字面值的操作数(例如ARM,在寄存器中)。

加载其中一个值为1的寄存器后,其他

机器可能需要使用加注册指令。


这一切都取决于。


-

当我与他交谈时,巫师似乎非常愿意。 / Howl's Moving Castle /


Hewlett-Packard Limited Cain Road,Bracknell,注册号:

注册办事处:Berks RG12 1HN 690597 England

No; it''s implementation-specific. ("Implementation-defined" is a
technical term of the C standard, meaning "the implementation must
do something about X /and tell you what it does/". Implementation-
specific isn''t a term of the Standard; I just mean that the
implementation does whatever it does and isn''t obliged to tell you
what that is.)

Some machines (eg PDP-11) have fancy "increment" instructions
which will add 1 to a register (or even a memory location).
Other machines have to use their ordinary "add" instruction with
an operand of literal-one (eg ARM, in regsisters). Yet other
machines might need to use an add-register instruction after
loading one of the registers with the value 1.

It All Depends.

--
"The wizard seemed quite willing when I talked to him." /Howl''s Moving Castle/

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England


s0****@gmail.com 写道:
s0****@gmail.com wrote:

我是C的新手,对+ =和++运算符有疑问。写作

''i + = 1''对''我'造成的影响与写''++ i'(或''i ++'',但
$相同b $ b获取的值将不同)。
I''m new to C and having doubt about the += and ++ operators. Writing
''i += 1'' causes the same effect on ''i'' as writing ''++i'' (or ''i++'', but
the fetched value will be different).



是的。但是+ =表示法允许你向''i'添加任意值。

增量运算符总是加一个。

Yes. But the += notation allows you to add any arbitrary value to ''i''.
The increment operators always add one.


但是因为''i + = 1''相当于''i = i + 1''相当于
,所以它是一个重新分配,对吗?那么,

写''++ i'还是''i ++''也是一个重新分配?
But since ''i += 1'' is rather
equivalent to ''i = i + 1'', then it is a reassignment, right? So, is
writing ''++i'' or ''i++'' also a reassignment?



抽象意义上是。但是对于i + = 1而不是++ i,编译器可能会也可能不会发出相同的

指令。

In an abstract sense yes. But the compiler may or may not emit the same
instructions for i += 1 versus ++i.


编译器在做什么

内部增加该值的值?或者是实现定义的行为?b
实现定义的行为?
What is the compiler doing
internally to increment the value in that case? Or is that
implementation-defined behavior?



它完全实现了这些语句与其目标代码指令之间的对应关系。

增量和

减量运算符的引入使得某些实现可以使用更有效的指令(INC和DEC)来为它们提供
与通用ADD或SUB指令进行比较。实际发生的事情

取决于系统的特性和编译器的

优化设置。

It''s completely implementation defined as to the correspondance between
these statements and their object code instructions. The increment and
decrement operators were introduced so that some implementations could
take advantage of more efficient instructions (INC and DEC) for them in
comparison to a generic ADD or SUB instruction. What actually happens
depends on the characteristics of your system and your compiler''s
optimisation settings.

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

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