开发者

confusion about static variables

开发者 https://www.devze.com 2023-01-08 21:57 出处:网络
I have a confusion开发者_高级运维 in the concepts of static integer.When i initialize a static integer in main function i.e

I have a confusion开发者_高级运维 in the concepts of static integer.When i initialize a static integer in main function i.e

static int i;

Now the static integer is assigned a value 0.Now in the next step:

i++;

i becomes 1.

Now the program terminates. I want to know what program will produce on its' next run. Also, what would happen if the entire program is closed? I understand that the first line is static int i; thus the next value when the function is next run should retain the value of i when it was previously run. If so, what is the advantage of making a variable static? Does the variable have a time limit or can it be stored forever? What would the value be if I ran the function again?


In C, "static" means that the variable has local scope within global storage.

The scope of variables in C is a block. In other words variables can be used inside the block they are declared. And normally they just keep their values until the block ends, being lost after that. Example:

{
   int a; 
   // Only a can be used here
   {
      int b;
      // a and b can be used here
      {
         int c;
         // a,b and c can be used here
      }
      //just a and b can be used here. c is not available anymore
    }
    // only a can be used here. Neither b nor c are available anymore
 }

This is true except for global variables that can be used all along the program.

The other exception is the static variable. It is only seen inside the block but keeps its value after the block is over.

This means that if you declare a static variable inside a function, it will maintain its value between function calls.

For example, the function below has a local variable. Local variables have scope of block (this means you can only access the variable 'var' inside the block {} it is declared, in the case below inside the function):

void countFunction(void)
{
  int var = 0;
  var = var + 1;
  printf("Value is %d\n", var);
}

Once the variable is not static, every time you call the function it will print "Value is 1" because the variable is stored in the stack that is allocated on the function call and deallocated after the function returns.

If you change var to be static,

void countFunction(void)
{
  static int var = 0;
  var = var + 1;
  printf("Value is %d\n", var);
}

The first time you call the function var will be initialized as 0 and the function will show "Value is 1". Nevertheless, at the second time, var will be already allocated and at a global area. It will not be initialized again and the function will display "Value is 2".

This within the program execution.

Although a static variable is allocated as long as your program executes, it does not keep its value after your program finishes (the program will free all of its memory). The only way to keep any value for the next run is to store it at a non-volatile media like the disk.

Hope it helps.


It will be 0 again because with termination of program all memory/variables are lost.

so what is the advantage of making it static?

It is useful as long as you do not terminate your program :) It can for example be used to count how many times a function is called if you use a static variable in that function and increment it with each call. This is just a simple example but there could be more practical uses of it of course.

More Readings:

C Static Variables


The value of i will be 0 when you start the application the next time. The inialization to 0 is a default inialization (which is very uncommon in the rest of C) that happens at program startup.

How'd you imagine it would be stored anywhere between execution? Whenever your application is terminated, it's memory will be reclaimed by the Operating System. Therefore the memory block that stored i will be lost too.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号