Question

Since b++ is post-increment,what happens to the increment of b if used as return b++ as in the following program?

#include<stdio.h>

int foo(int);

int main()
{
   int a=8;
   printf("%d",foo(a));
}

int foo(int a)
{
   static int  b=a*a;
    return b++;
}

Edit

#include<stdio.h>
int foo();

int main()
{
foo();
foo();
}

int foo()
{
    static int b=1;
    printf("%d\n",b);
    return b++;
}

Result

1

2

As I saw in my edit, why is b incremented at all?Isn't return supposed to exit that function immediately?Why is b incremented even after control returns to main()?Aren't all activities in the function supposed to end after return?

Was it helpful?

Solution

Many C (sub-)expression have a value and a side effect.

The value of b++ is the value of b before evaluating the expression; its side effect is to increase the value in b by one.

So, the expression return b++; returns the previous value of b and updates b. When b is static the update stays around for the next function call; when b is a plain old local variable, the update is lost (a smart compiler will even not emit code to update the object).

OTHER TIPS

Actually, this code doesn't compile, because b's initializer isn't a constant.

Since b is static, it's essentially a global variable, and it gets initialized before main, at which point a doesn't exist.

If on the other hand we compile this as C++, b is initialized the first time that foo is called - so gets the value 64. At the return, b++ is incremented and stored as 65 - but the return value is 64. So if you call foo again, it will return 65 (and b is 66).

Edit based on edited code:

So, the code essentially performs:

 int temp = b;
 b = b + 1;
 return temp; 

This is the way that C is define. The result of x++ is the previous value x, but the value of x is increment. Since static is essentially a global (but without a name, so you can't use it outside of that function), the value persists between calls to the function, and it is initialized before main is called.

return b++; is equivalent to: int c = b; b = c + 1; return c;

To answer your new questions: - The function exits after return indeed, but before it returns, the expression b++ must be evaluated first. Evaluating such an expression will result in b being incremented. - All activities in the function "end" after return but b is declared as a static variable, in which case its value persists through subsequent executions of the function.

To make it easier for you to understand, a post increment is like the following function

int postincrement(int& x) {
    int y = x;
    x = x + 1;
    return y;
}

(Of course the compiler could optimize a b++ if it finds that incrementing b has no effect at all, but in this case it does have an effect (increment the static int b) so it can't be optimized out.)

This post-increment is completely useless - because b has local scope, its incremented value is disposed after return. If your compiler is smart, it most likely to optimize it out.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top