Question

I know that the variable that is declared static only has scope in the file in which it is declared whereas the variable declared without static can be accessed from other files using an extern declaration.

but Is it true that the static "Global" is to make the variable retain the last assigned value. Like the Static Local Purpose ?

#include <stdio.h>

void func() {
  static int x = 0; // x is initialized only once across three calls of func()
  printf("%d\n", x); // outputs the value of x
  x = x + 1;
}

int main(int argc, char *argv[]) {
  func(); // prints 0
  func(); // prints 1
  func(); // prints 2
  return 0;
}
Was it helpful?

Solution

The term "maintaining last value" causing the confusion for you. The term "maintaining last value" would be used on the context of function local variable vs function static variable. Because variable declared within a function got stored in stack, and on exiting the function will be cleared the stack used by that function which leaves the variable dead.

But creating a function local variable with static is creating a memory in RAM(and not in stack), which allows the memory to hold even after exit of the function. Even the global variables and file static variables are stored in RAM thereby retaining its value forever.

So, for your question any variable stored in RAM will "maintain its last value" irrespective of its keyword static. But the static limits its scope of its usage, ie., if declared in the file scope then it can be used(accessed) within the file, if declared in the function scope then its scope limits within the function alone.

OTHER TIPS

For the first time, when the function func is called, x will be initialized to zero. But from the next call of func, x = 0 will not called, as x is already initialized.

Have added int y to your code in this example y is available (i.e. addressable) from both main() and to func().

Please note, I have NOT added any other code to your program, and some will point out that if actually compiled y might get flagged as not being used. But for this discussion, both main and func can set and reference y.

If you can avoid using a Global Var then do so, but in C they can be useful. K&R discusses this very point.

Hope this helps.

Within the scope of a function, the static initializer is all about duration. The variable is initialized only once, at or before the first time the function is called, (and if the code doesn't specify an initialization value, then the variable is initialized with 0, or NULL, or appropriate equivalent. After that, the variable continues to exist for the duration of the program run. The scope of the identifier is limited to the function, but the variable itself continues to exist, so it is valid to return a pointer to it, which would otherwise be illegal. (The "scope of the identifier" is the name of the variable itself. If you have a function named "gimme_a_string" and inside that function you have static char my_str[20], the name my_str only exists inside the function, but the 20-byte array always exists.)

Outside of a function scope ("at file scope"), any variable you declare is already going to have duration the same as a full run of the program. In this context, declaring a variable static affects what is called linkage. If at file scope you declare static int my_flags, the variable name my_flags can not be seen by any other "translation unit". Any other source files in your program, or any library, that tries to get your my_flags by saying extern int my_flags, won't work. Any function in your file can still give a pointer to this variable to any function that calls it, but the variable name isn't visible to any outside code.

THEREFORE: The answer to your question is "No, it is not true that at file scope, declaring a variable as static has the effect of making the variable retain the last-assigned value, as it does in function scope. Any file-scope variable will have that property, declaring it static simply hides the symbol from other translation units."

global static variables have static initialization. They can only be accessed in the file where it is created. If you don't assign a value to them, they will be initialized to 0.
Also if global variables are defined outside of function, their scope starts at the point where they are defined and lasts to the end of the file.

local static variable is a variable that can maintain its value from one function call to another, and it will exist until the program ends.

#include <stdio.h>
int global_var;
static int static_var;
void func() {
 static int x = 0; // x is initialized only once across three calls of func()
 printf("%d\n", x); // outputs the value of x
 x = x + 1;
}

int main(int argc, char *argv[]) {
func(); // prints 0
func(); // prints 1
func(); // prints 2

return 0;
}

global_var and static_var have a value of 0 when the program runs.
When program ends their values ​​remain 0.
x inside a func(), means that object exists from the first time the function is called until the program ends.

A local static variable is a variable that can maintain its value from one function call to another and it will exist until the program ends.When a local static variable is created, it should be assigned an initial value. If it's not, the value will default to 0.

A global static variable is one that can only be accessed in the file where it is created. This variable is said to have file scope.

The difference between static global and static local is mostly what is between a 'normal' global and a 'normal' local. You cannot access a static local outside the function in which it is declared but a static global is accessible throughout all functions within the file in which the static global is defined.

The main difference between the module static and global(extern) variable is static local variables are stored in LI_1 area of RAM whereas global variables are stored in PUBLIC area of RAM. Hence the global variables are accessible from other modules and static variables are not. Apart from that with respect to the same module both behave the same way. Both will be initialized to zero and retain the value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top