C语言 从任何角度来看,++i 和 i+=1 之间有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18420479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:15:16  来源:igfitidea点击:

what is difference between ++i and i+=1 from any point of view

coperators

提问by chanzerre

This is a question from kn king's c programming : a modern approach. I can't understand the solution given by him:-

这是来自 kn king's c programming 的一个问题:一种现代方法。我无法理解他给出的解决方案:-

The expression ++i is equivalent to (i += 1). The value of both expressions is i after 
the increment has been performed.

How do I understand this anyway?

无论如何,我如何理解这一点?

回答by thefourtheye

i = 10
printf("%d", i++);

will print 10, where as

将打印 10,其中

printf("%d", ++i);

will print 11

将打印 11

X = i++can be thought as this

X = i++可以这样认为

X = i
i = i + 1

where as X = ++iis

其中,作为X = ++i

i = i + 1
X = i

so,

所以,

printf ("%d", ++i); 

is same as

printf ("%d", i += 1);

but not

但不是

printf ("%d", i++);

although value of iafter any of these three statements will be the same.

尽管i这三个语句中任何一个之后的值都是相同的。

回答by user4815162342

The solution means to say that there isno difference, ++ihas the same meaning as (i += 1)no matter what ihappens to be and no matter the context of the expression. The parentheses around i += 1make sure that the equivalence holds even when the context contains further arithmetics, such as ++i * 3being equivalent to (i += 1) * 3, but not to i += 1 * 3(which is equivalent to i += 3).

该解决方案的意思是说,有没有什么区别,++i具有相同的含义(i += 1)不管什么i恰好是并不管表达的情况下。周围的括号i += 1确保即使上下文包含进一步的算术,例如++i * 3等效于(i += 1) * 3,但不i += 1 * 3等效于(等效于i += 3),等价也成立。

The same would not apply to i++, which has the same side effect (incrementing i), but a different value in the surrounding expression — the value of ibefore being incremented.

这同样不适用于i++,它具有相同的副作用(递增i),但周围表达式中的值不同 -i递增之前的值。

回答by user1952500

One difference that has not been brought up so far is readability of code. A large part of loops use increment by one and common practice is to use i++/++i when moving to the next element / incrementing an index by 1.

到目前为止还没有提出的一个区别是代码的可读性。大部分循环使用 1 递增,通常的做法是在移动到下一个元素/将索引递增 1 时使用 i++/++i。

Typically i+= is used in these cases only when the increment is something other than 1. Using this for the normal increment will not be dangerous but cause a slight bump in the understanding and make the code look unusual.

通常,仅当增量不是 1 时才在这些情况下使用 i+=。将它用于正常增量不会有危险,但会导致理解上的轻微颠簸并使代码看起来不寻常。

回答by naveenKumar

Difference between both are:++ is a unary operator while + is a binary operator.... If we consider execution time:i++ is more faster than i=i+1.No of machine cycles differs to execute the same set of code, thats the reason ++ operators are always prefered for Loops. Refer to this thread for more info

两者之间的区别是:++ 是一元运算符,而 + 是二元运算符…… 如果我们考虑执行时间:i++ is more faster than i=i+1.执行同一组代码的机器周期数不同,这就是 ++ 运算符总是首选的原因循环。 有关更多信息,请参阅此线程

回答by Mike

I think they are totally the same. There is one thing maybe interesting. The ++i is equal to (i+=1) but not i+=1; The difference is the brace. Because i += 1 may depend on the context and it will have different interpretation.

我认为他们是完全一样的。有一件事可能很有趣。++i 等于 (i+=1) 但不等于 i+=1;不同之处在于支架。因为 i += 1 可能取决于上下文,它会有不同的解释。

回答by carlos

In a normal operation without assignation:

在没有赋值的正常操作中:

++i and i++

++我和我++

increase the variable in 1. In pseudo assembly both code it is:

在 1 中增加变量。在伪汇编中,这两个代码都是:

inc i

but If you assign the value the order of ++ is relevant:

但是,如果您分配该值,则 ++ 的顺序是相关的:

x = i++

x = i++

produce:

生产:

mov x, i
inc i

x = ++i

x = ++i

produce:

生产:

inc i
mov x, i

In the case of: i += 1

在以下情况下: i += 1

it will produce:

它将产生:

add i,1

but because compilers optimize the code it also will produce in this case:

但是因为编译器优化了代码,它也会在这种情况下产生:

inc i

回答by phaazon

++iis the pre-increment operator. It increments ibefore setting and returning the value (which is obviously i + 1).

++i是预增量运算符。它i在设置和返回值之前递增(显然是i + 1)。

Now, i++is the post-increment operator. It increments iafter the whole instruction it appears in is evaluated.

现在,i++是后增量运算符。它i在它出现的整个指令被评估后递增。

Example:

例子:

int i = 0;
std::cout << ++i << std::endl; /* you get 1 here */
std::cout << i++ << std::endl; /* you still get 1 here */
std::cout << i << std::endl;   /* you get 2 here */