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

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

Output: 3 2 1 (Order depends upon compiler)

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.

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)

Elazar
  • 20,415
  • 4
  • 46
  • 67
codey modey
  • 983
  • 2
  • 10
  • 23
  • order is undefined..but why is the value returned 1? – codey modey Feb 16 '14 at 08:11
  • You need to read about [sequence points](http://en.wikipedia.org/wiki/Sequence_point). – devnull Feb 16 '14 at 08:13
  • @codeymonkey Read http://stackoverflow.com/questions/17366847/what-is-the-difference-between-pre-increment-and-post-increment-in-the-cycle-fo – Daksh Shah Feb 16 '14 at 08:13
  • See [this](http://stackoverflow.com/questions/949433/why-are-these-constructs-undefined-behavior) and [this](http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points). – devnull Feb 16 '14 at 08:14
  • yes, I know I am in a learning phase, but still my name is codey modey..not codeymokey..lol – codey modey Feb 16 '14 at 08:15
  • @codeymodey Does my answer satisfy your doubt? – Daksh Shah Feb 16 '14 at 08:22
  • @devnull, no it is not undefined behavior. A function call is a sequence point. – Jens Gustedt Feb 16 '14 at 08:23
  • @JensGustedt The order in which arguments to a function (`printf` in this case) are evaluated is _unspecified_. Isn't it? – devnull Feb 16 '14 at 08:42
  • Yes, it is undefined, depends upon compiler – codey modey Feb 16 '14 at 08:45
  • No this isn't __undefined__, it is __unspecified__. – devnull Feb 16 '14 at 08:48
  • The accepted answer here is plain wrong. The standard says that `The order in which the arguments to a function are evaluated` is __unspecified__. – devnull Feb 16 '14 at 08:50
  • @devnull my question was not on order of evaluation, my question was on post increment confusion in return expressions – codey modey Feb 16 '14 at 08:55
  • @codeymodey If it was post-increment, even then the answer doesn't make any sense. Both the question and the answer are unlikely to help any future visitors. – devnull Feb 16 '14 at 08:58
  • @devnull feel free to delete and edit it..my doubt is resolved..if you think there is something needs to edited or added let me know..vote it down, i will delete it in a while..I am not that intelligent to judge my questions(generally too basic)..pretty small in front of you guys.. – codey modey Feb 16 '14 at 09:07

1 Answers1

0

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().

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().

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

Source: Answer at this place

Update 1

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)

Update 2

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)

Community
  • 1
  • 1
Daksh Shah
  • 2,997
  • 6
  • 37
  • 71
  • Unfortunately the top answer in the linked question regarding pre vs post increment is wrong. That isn't how it works, though it is often taught that way in academia (again, unfortunate). 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. A sample program demonstrating this is fairly straight forward if interested. It is the temp-copy that makes post-inc expensive. – WhozCraig Feb 16 '14 at 08:24
  • 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 value. – codey modey Feb 16 '14 at 08:27
  • 1
    @codeymodey 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. – WhozCraig Feb 16 '14 at 08:34