0

First take a look:

#include <stdio.h>

static int v1 = 0;

int fun()
{
    static int v2 = 0;
    ++v2 && ++v1;
    printf("%i\n", v2);
    return v2;
}
int main()
{
    ++v1;
    printf("%i\n", fun());
    printf("%i\n", v1);
}

OUTPUT:

1
1
2

So the whole thing is about global static & local static variables in C, so the main property of the static variable is the fact that it's "Preserving it's value", but here it doesn't, the first piece of output is as expected : the value of v2 in fun() should ++v2 which is 1 but the second piece is not, what expected is when it called by main() it's preserved value would be 1 and it would again ++v2 so the second output expected to be 2 . When we eliminate return v2 the program works as expected.

#include <stdio.h>

static int v1 = 0;

int fun()
{
    static int v2 = 0;
    ++v2 && ++v1;
    printf("%i\n", v2);
}
int main()
{
    ++v1;
    printf("%i\n", fun());
    printf("%i\n", v1);
}

OUTPUT:

1
2
2

The question is Why ? thanks.

Question
  • 63
  • 7
  • You only call the function once. How can you tell whether it's preserving the value between calls? – Barmar Jun 05 '21 at 15:28
  • If you remove `return v2;` you have undefined behavior, because the function is declared to return something. – Barmar Jun 05 '21 at 15:30
  • i see, the function is undefined "kind of", yet still it's returning a value , the value of variable ```v2``` and that variable is statically preserving it's value in the first ```++v2```, while when there's a return statement that variable is not keeping it's value, for your first comment the variable is called twice in in the function it's self and outside of it. – Question Jun 05 '21 at 15:38
  • The value of the variable is `1` after `++v2`. Why do you think it's not preserving that? – Barmar Jun 05 '21 at 15:40

2 Answers2

1

main() increments v1, so its value is 1.

When you call fun(), it increments v2 and v1, so v2 is 1 and v1 is 2.

Then it prints v2, so it prints 1. This is the first line of output.

Then it returns v2, so it returns 1.

Then main() prints the return value, so it prints 1. This is the second line of output.

Then main() prints v1, so it prints 2. This is the third line of output.

You don't call fun() twice, so nothing in your snippet depends on whether the value is preserved. In order to see that the variable is preserved, you have to call the function again. If you add another

printf("%d\n", fun());

at the end it will print 2 twice, because the value of v2 will be preserved and incremented.

Your second code snippet produces undefined behavior because the function that's declared to return an int doesn't return anything. It's only accidentally returning what you expect -- 2 is the return value of printf(), since it returns the number of characters that it printed (1 followed by a newline), and that's getting left in the location that's used for the return value of the function. Change the function to do

printf("|%d|"\n", v2);

and I expect you'll get different results.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • yeah exactly like you said Barmar, when the function called without return ,t printed how many elements are in printf function accidentally 2 thanks for reaching out and for the good explanation – Question Jun 05 '21 at 15:54
0

The first code block with the return statement is correct behaviour. The flow of the program goes this way:

  1. Compiler starts from main()
  2. v1 = v1 + 1; Therefore v1 = 1
  3. You're calling fun() in your print statement --> program control goes to fun():
    1. v2 is initialized
    2. v1 and v2 are incremented, v2 = 1 ; v1 = 2; (This part is actually tricky if the first value in the && statement turned out to 0)
    3. prints 1 to console
    4. returns the value iof v2 which is 1 to the calling function; in this case main()
  4. Control is back to the main; Since you are printing the return value of the function it prints 1
  5. The last statement prints the value of v1 which is 2.

Therefore the output is

1
1
2

In the second block of code, you might have just gotten lucky. The function signature says it returns an int whereas you return nothing. This leads to erratic/undefined behaviour. Unfortunately C as a language doesn't enforce this on the programmer.

For more details on the reasons of this undefined behaviour which is due to an eax register, refer : Function returns value without return statement

Shashank Ks
  • 450
  • 5
  • 9
  • yeah just accidentally printed 2 which is how many elements are in the ```printf()``` function ```1``` and feed line ```\n```, thanks for the reference. – Question Jun 05 '21 at 15:59