对于后增量 [英] Regarding post increment

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

问题描述

   int get()
   {
      static i = 1;
      return i++;
   }

  int main(int argc, char *argv[])
  {
      printf("%d %d %d\n", get(), get(), get());
      return 0;
  } 

输出:3 2 1(顺序取决于编译器)

Output: 3 2 1 (Order depends upon compiler)

问:但是为什么前值增量返回的静态变量(文件范围)的。什么是后/ pre增量的拇指规则?我从来没有得到它正确的。请帮忙。

Question: But why is the value before increment returned of the static variable (file scope). What is the thumb rule of post/pre increment? I never get it correct. Please help.

好吧,让我更具体,那我读都喜欢, A =我++的所有例子;或= ++我; 这些都是前pressions递增然后分配或分配,然后递增。但是,什么样的前pressions的这些,回我++; FUNC(A ++); 我的理解是这样的后,我什么都不++分配,因此返回最终增加值(指正在这里)

Okay, let me be more specific, all the examples that I read are like, a = i++; or a = ++i; these are the expressions to increment then assign or assign then increment. But what kind of expressions are these, return i++; func(a++); I read it like this "after i++ nothing to assign, so return the final incremented value" (correct me here)

推荐答案

有两个问题在这里,寿命和范围。

There are two issues here, lifetime and scope.

变量的范围是其中变量名就可以看出。在这里,我是唯一可见里面的功能得到()。

The scope of variable is where the variable name can be seen. Here, i is visible only inside function get().

的变量的生存期是在其上存在的时间段。如果我是没有static关键字定义的,寿命是从进入的get()从中获取回报();所以它会被重新初始化到1在每次调用。

The lifetime of a variable is the period over which it exists. If i were defined without the keyword static, the lifetime would be from the entry into get() to the return from get(); so it would be re-initialized to 1 on every call.

关键字静态行为来扩展变量的程序的生存的生存期;例如初始化发生一次,只有一次,然后该变量保留其值 - 不管它来是 - 在所有未来的调用来获得()

The keyword static acts to extend the lifetime of a variable to the lifetime of the program; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to get().

职位和pre增量之间的差异:<一href=\"http://stackoverflow.com/questions/17366847/what-is-the-difference-between-$p$p-increment-and-post-increment-in-the-cycle-fo\">What在循环pre-递增和后递增的区别(用于/时间)?

Difference between post and pre increment: What is the difference between pre-increment and post-increment in the cycle (for/while)?

来源:答案在这个地方

更新1

发表增量的工作原理是使现有价值的临时副本,然后递增原始值,然后最后返回临时作为前pression的结果。其结果是,它出现在增量做的事后,pression评价,但它不是,和示例程序演示,这是相当简单的,如果有兴趣。这是临时复制,使-INC后昂贵。
(感谢WhozCraig纠正)

Post increment works by making a temporary copy of the existing value, then incrementing the original value, then finally returning the temporary as a result of the expression. As a result, it appears the increment is done post-expression evaluation, but it isn't, and a sample program demonstrating this is fairly straight forward if interested. It is the temp-copy that makes post-inc expensive. (Thanks to WhozCraig for correcting)

更新2

。这都是滞后递增一元操作。他们都使操作数的一个临时副本(我在第一种情况中,在第二个),然后增加操作数,然后返回临时副本的增量后的前pression的结果。在第一种情况下的结果是i递增并返回其增量之前的值。在第二种情况下一个递增和func被调用之前用增量的值。(由WhozCraig鉴于)

Both of those are post-increment unary operations. Both of them make a temp copy of the operand (i in the first case, a in the second), then increment the operand, then return the temp copy as the result of the post-inc expression. The result in the first case is i is incremented and its value prior to the increment is returned. In the second case a is incremented and func is invoked with the value prior to the increment.(Given by WhozCraig)

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

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