Static variable inside of a function in C

2019-01-03 08:48发布

What will be printed out? 6 6 or 6 7? And why?

void foo()
{
    static int x = 5;
    x++;
    printf("%d", x);
}

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

标签: c static
12条回答
戒情不戒烟
2楼-- · 2019-01-03 09:39

Output: 6,7

Reason

The declaration of x is inside foo but the x=5 initialization takes place outside of foo!

What we need to understand here is that

static int x = 5;

is not the same as

static int x;
x = 5;

Other answers have used the important words here, scope and lifetime, and pointed out that the scope of x is from the point of its declaration in the function foo to the end of the function foo. For example I checked by moving the declaration to the end of the function, and that makes x undeclared at the x++; statement.

So the static int x (scope) part of the statement actually applies where you read it, somewhere INSIDE the function and only from there onwards, not above it inside the function.

However the x = 5 (lifetime) part of the statement is initialization of the variable and happening OUTSIDE of the function as part of the program loading. Variable x is born with a value of 5 when the program loads.

I read this in one of the comments: "Also, this doesn't address the really confusing part, which is the fact that the initializer is skipped on subsequent calls." It is skipped on all calls. Initialization of the variable is outside of the function code proper.

The value of 5 is theoretically set regardless of whether or not foo is called at all, although a compiler might optimize the function away if you don't call it anywhere. The value of 5 should be in the variable before foo is ever called.

Inside of foo, the statement static int x = 5; is unlikely to be generating any code at all.

I found the address x uses when I put a function foo into a program of mine, and then (correctly) guessed that the same location would be used if I ran the program again. The partial screen capture below shows that x has the value 5 even before the first call to foo.

Break Point before first call to foo

查看更多
We Are One
3楼-- · 2019-01-03 09:41

That is the same as having the following program:

static int x = 5;

void foo()
{
    x++;
    printf("%d", x);
}

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

All that the static keyword does in that program is it tells the compiler (essentially) 'hey, I have a variable here that I don't want anyone else accessing, don't tell anyone else it exists'.

Inside a method, the static keyword tells the compiler the same as above, but also, 'don't tell anyone that this exists outside of this function, it should only be accessible inside this function'.

I hope this helps

查看更多
手持菜刀,她持情操
4楼-- · 2019-01-03 09:45

You will get 6 7 printed as, as is easily tested, and here's the reason: When foo is first called, the static variable x is initialized to 5. Then it is incremented to 6 and printed.

Now for the next call to foo. The program skips the static variable initialization, and instead uses the value 6 which was assigned to x the last time around. The execution proceeds as normal, giving you the value 7.

查看更多
贼婆χ
5楼-- · 2019-01-03 09:46

Vadiklk,

Why ...? Reason is that static variable is initialized only once, and maintains its value throughout the program. means, you can use static variable between function calls. also it can be used to count "how many times a function is called"

main()
{
   static int var = 5;
   printf("%d ",var--);
   if(var)
      main();
} 

and answer is 5 4 3 2 1 and not 5 5 5 5 5 5 .... (infinite loop) as you are expecting. again, reason is static variable is initialized once, when next time main() is called it will not be initialize to 5 because it is already initialized in the program.So we can change the value but can not reinitialized. Thats how static variable works.

or you can consider as per storage: static variables are stored on Data Section of a program and variables which are stored in Data Section are initialized once. and before initialization they are kept in BSS section.

In turn Auto(local) variables are stored on Stack and all the variables on stack reinitialized all time when function is called as new FAR(function activation record) is created for that.

okay for more understanding, do the above example without "static" and let you know what will be the output. That make you to understand the difference between these two.

Thanks Javed

查看更多
贼婆χ
6楼-- · 2019-01-03 09:50
6 7

x is a global variable that is visible only from foo(). 5 is its initial value, as stored in the .data section of the code. Any subsequent modification overwrite previous value. There is no assignment code generated in the function body.

查看更多
放荡不羁爱自由
7楼-- · 2019-01-03 09:51

A static variable inside a function has a lifespan as long as your program runs. It won't be allocated every time your function is called and deallocated when your function returns.

查看更多
登录 后发表回答