static storage class working in c

2020-03-07 06:46发布

问题:

I'm a beginner so please bear with me. Recently, I started reading storage classes in C and I stumbled upon this question:

#‎include‬<stdio.h>
int fun()
{
static int num = 16;
return num--;
}
int main()
{
for(fun(); fun(); fun())
printf("%d \n", fun());
return 0;
}

This program outputs : 14 11 8 5 2.

1) Can any please tell me how does this code work?

2) When I keep --num in fun() ,it is running an infinite loop. Why it happens like that?

回答1:

static int num = 16; means that num will be initialized as 16 and it will not be destroyed when the function returns.

return num--; means that num value will be returned but after that num value will be decreased and saved because num is declared as static.

I MARKED different calls to fun() with numbers (just to follow execution flow, not to be used as real code) so it could be shown how variable num is changing.

for(fun1(); fun2(); fun4())
    printf("%d \n", fun3());

fun1() "is called" only once as initialization. fun2() is a control expression, if the result is zero than execution of for loop stops. fun3() "is called" each time in the loop. fun4() "is called" each time at the end of loop"

How values are changing:

fun1() called
    num: 16

fun2() called
    num: 15
fun3() called
    num: 14
14
fun4() called
    num: 13

fun2() called
    num: 12
fun3() called
    num: 11
11
fun4() called
    num: 10

fun2() called
    num: 9
fun3() called
    num: 8
8
fun4() called
    num: 7

fun2() called
    num: 6
fun3() called
    num: 5
5
fun4() called
    num: 4

fun2() called
    num: 3
fun3() called
    num: 2
2
fun4() called
    num: 1

fun2() called
    num: 0      ==> stop

If you change num-- to --num than for loop control expression (marked as fun2()) never gets 0.



回答2:

Well, first thing i hope you knows what is the Storage Class in C.Now when we are dealing with static variable, This variable is persists until the end of the program and stored in data segment.Some characteristics of static variable are as follow:

Storage = memory

Default initial value = Zero

Scope = Local to the block in which the variable is defined.

Life = Value of the variable persists between different function call.

So now coming to your question.

  1. Working of code:Well for this we will start from main(). Thinking at compiler level, first focus will be at for loop. In the for loop, one time initialization will be carried out. Now num = 15.Then it will check condition given. Now for C, it will only compare Zero and Non-Zero values. Now for this it returns 14 and it is non-zero so it get inside the loop. Now the magic of compiler get began. Read this for some information. so in your case wile returning it will first return value and then decremented value by 1.so first 14 will be printed , inc/dec block in for loop will get executed.Then again condition will gets evaluated. And again , while printing your function will return value first then decremented by 1. And after all this iteration your output will be there.

  2. When you wrote --num ,then simple thing is it will decremented value by 1 first and then return a value. Now as mentioned earlier, Compiler only checks Zero and Non-Zero values. Whn you are dealing with --num then it goes into negative values and it still got decremented its value, so it will never meet at Zero. so resultant infinite loop. You can modify some values to check your results like modify num=17 for --num, You should probably get same result.



回答3:

There is a nice post on this site that goes through static variables: What does "static" mean?

But basically a static variable keeps its value throughout the life of the program.

I will step through the code with you, but a good resource in future to do this with is gdb: https://www.gnu.org/software/gdb/

int fun()
{
static int num = 16; /* Essentially this line is only seen once by the program.
                     ** The 'num' variable keeps its value for the life of the program. 
                     */ 
return num--; /* Returns the value of 'num' and *afterwards* subtracts 1 from 'num'. */
}
int main()
{
for(fun(); fun(); fun())
printf("%d \n", fun()); /* This line runs the for loop  until 'num' == -1, as the 
                        ** condition is fun(), which is true while it returns a 
                        ** value > 0. fun() is run twice when the loop starts, once in 
                        ** the intialising part of for() (the first term), then once by 
                        ** the conditional term (the middle term). From there on it is 
                        ** run once by the printf(), once by the updating term 
                        ** (the end term), and once by the conditional term, 
                        ** until the conditional term is not fulfilled. 
                        */
return 0;
}

As for why it doesn't run when return --num; is the last line in fun(), it is because the conditional statement in the for loop will never receive a 0 (0 and only 0 is false, every other number is true) . The outputs of the program would be: 13, 10, 7, 4, 1, -2, etc; meaning the conditional statement will receive: 14, 11, 8, 5, 2, -1, etc.



标签: c storage