Static variable inside function in C

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

void foo() { static int x = 5; x++; printf("%d", x); } int main() { foo(); foo(); return 0; } 
+89
c static
Feb 17 '11 at 19:31
source share
13 answers

There are two questions here: lifetime and scope.

The variable name displays the variable name. Here x is visible only inside the foo () function.

The lifetime of a variable is the period during which it exists. If x is defined without the static keyword, the lifetime will be from entering foo () to returning from foo (); therefore, it will be reinitialized to 5 on every call.

The static keyword acts to extend the lifetime of a variable by the lifetime of a program; for example, initialization occurs only once, and then the variable retains its value - regardless of what happened - over all future calls to foo ().

+140
Feb 17 '11 at 19:34
source share

Output : 6 7

Reason : a static variable is initialized only once (as opposed to an automatic variable), and further definition of the static variable will be bypassed at run time. And if it is not initialized manually, it is initialized to 0 automatically. So,

 void foo() { static int x = 5; // assigns value of 5 only once x++; printf("%d", x); } int main() { foo(); // x = 6 foo(); // x = 7 return 0; } 
+32
May 21 '14 at 8:24
source share

The same as the following program:

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

All the static keyword does in this program is telling the compiler (essentially): "Hi, I have a variable here that I don’t want anyone else to access it, don’t tell anyone else that it exists "

Inside the method, the static keyword tells the compiler the same as above, but also: "Do not tell anyone that this exists outside this function, it should be available only inside this function."

I hope this helps

+9
Feb 17 '11 at 19:37
source share

6 7

the compiler determines that static initialization of variables does not occur every time a function is entered

+8
Feb 17 '11 at 19:34
source share

A static variable inside a function has a lifespan while your program is running. It will not be allocated every time your function is called and freed when the function returns.

+5
Feb 17 '11 at 19:49
source share

The output will be 6 7 . A static variable (whether it is inside or not) is initialized exactly once before any function is executed in this translation block. After that, it retains its value until change.

+2
Feb 17 '11 at 19:35
source share

Vadiklk,

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

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

and the answer is 5 4 3 2 1, not 5 5 5 5 5 5 .... (endless loop) as you expect. again, the reason the static variable is initialized once, the next time main () is called, it will not be initialized to 5, since it is already initialized in the program. Thus, we can change the value, but cannot reinitialize. The way a static variable works.

or you can think of it as storage: static variables are stored in the data section of the program, and variables stored in the data section are initialized once. and before initialization, they are stored in the BSS section.

In turn, automatic (local) variables are stored in Stack, and all variables in the stack are reinitialized all the time when the function is called, since a new FAR (function activation record) is created for it.

in order to understand, follow the above example without “static” and let us know what the output will be. This will help you understand the difference between the two.

Thanks Javed

+2
Dec 05 '13 at 9:01
source share

Let's just read the Wikipedia article on static variables ...

Static local variables: variables declared as static inside a function are statically distributed, having the same scope as automatic local variables. Therefore, any values ​​that the function places in its static local variables during a single call will still be present when the function is called again.

+1
Feb 17 '11 at 19:34
source share

You will get 6 7 printed as it is easy to check, and here's the reason: When foo is called first, 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 initialization of the static variable and instead uses the value 6, which was assigned x for the last time. The execution runs as usual, giving you a value of 7.

+1
Feb 17 '11 at 19:36
source share
 6 7 

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

+1
Feb 17 '11 at 19:37
source share

6 and 7 Since the static variable intialise only once, Thus, 5 ++ becomes 6 on the first call 6 ++ becomes 7 on the 2nd call Note. When the 2nd call occurs, the value of x is 6 instead of 5, because x is a static variable.

+1
Jun 07 '14 at 5:25
source share

Output: 6.7

cause

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

We need to understand that

 static int x = 5; 

it is not the same as

 static int x; x = 5; 

Other answers used important words here, scope and lifetime, and indicated that scope 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 this makes x undeclared in x++; expression.

Thus, the static int x (sphere) part of the statement actually applies when you read it, somewhere inside the function, and only from there forward, and not above it inside the function.

However, x = 5 (lifetime) part of the instruction is the initialization of the variable and the OUTSIDE function occurs as part of the program load. When the program loads, the variable x with the value 5 changes.

I read this in one of the comments: "In addition, this does not apply to the really confusing part, which is that the initializer is skipped on subsequent calls." It is skipped for all calls. Initialization of a variable outside the function’s own code.

The value 5 is theoretically set regardless of whether foo is called at all, although the compiler can optimize the function if you don't name it. The value 5 must be in the variable before foo is called.

Inside foo statement is static int x = 5; is unlikely to generate any code at all.

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

Break point before first call to foo

+1
Mar 05 '18 at 11:35
source share

At least in C ++ 11, when the expression used to initialize the local static variable is not "constexpr" (cannot be evaluated by the compiler), then initialization should occur during the first call to the function. The simplest example is the direct use of a parameter to initialize a local static variable. Thus, the compiler must issue the code in order to guess whether the call is the first or not, which in turn requires a local logical variable. I compiled such an example and verified this by seeing the assembly code. An example could be this:

 void f( int p ) { static const int first_p = p ; cout << "first p == " << p << endl ; } void main() { f(1); f(2); f(3); } 

Of course, when the expression is constexpr, this is not required, and the variable can be initialized when the program loads, using the value stored by the compiler in the output code of the assembly.

0
Jan 11 '19 at 10:44
source share



All Articles