Is the static
keyword in C used only for limiting the scope of a variable to a single file?
I need to know if I understood this right. Please assume the following 3 files,
file1.c
int a;
file2.c
int b;
file3.c
static int c;
Now, if the 3 files are compiled together, then the variables "a" & "b" should have a global scope and can be accessed from any of the 3 files. But, variable "c" being static, can only be accessed from file3.c, right?
Does static
have any other use in C ? (other than to limit the scope of a variable as shown above?)
A variable may have three kinds of storage:
Global variables are always stored in static area. But to store a local variable in static area, you need the keyword
static
. As a static variable is not allocated on stack, you can access the variable on subsequent calls.Also
static
keyword at global scope gives a variable internal linkage.Consequently the variable cannot be accessed from some other file using theextern
qualifier.You are misusing the term "scope".
static
in C has absolutely nothing to do with scope.Scope is the region where the name of an entity (variable, function, typename etc.) is visible. In C language "file scope" is the largest scope ever. For that reason, there's no point in limiting anything to a single file: there's simply nothing larger to limit. There's no such thing as "global scope" in C. The term "global scope" is sometimes used informally, but in that case it has the same meaning as "file scope".
Again,
static
in C has absolutely nothing to do with scope.static
in C affects storage duration of an object and linkage of an identifier. When used with objects (variables)static
gives the object static storage duration (i.e. the object exists as long as the program runs). And, when used with identifiers of non-local objects or functions, it gives them internal linkage, meaning that the same identifier refers to the same entity within a single translation unit (where the entity is defined), but not in other translation units.The
static
keyword serves two distinct purposes in C, what I call duration (the lifetime of an object) and visibility (where you can use an object from). Keep in mind the C standard actually uses different words for these two concepts but I've found in teaching the language that it's best to use everyday terms to begin with.When used at file level (outside of any function), it controls visibility. The duration of variables defined at file level are already defined as being the entire duration of the program so you don't need
static
for that.Static variables at file level are invisible to anything outside the translation unit (the linker can't see it).
When used at function level (inside a function), it controls duration. That's because the visibility is already defined as being local to that function.
In that case, the duration of the variable is the entire duration of the program and the value is maintained between invocations of the function.
You are correct, this is called "static linkage": The symbol declared as
static
is only available in the compilation unit where it is defined.The other use of
static
would be inside a function:In this case the variable is only initialized once and keeps it's value through different calls of that function, like it would be a global variable. In this example the
counter
variable counts the number of times the function was called.static
is also used within a function definition to define a variable which keeps its value between function calls. I found an example here. In contrast, variables which are created anew with each function call are called automatic.internal linkage vs external linkage by example
//file1.c
when we will compile this program and run this program then os will load this program in memory.then below things will happened:
glb_var identifier will be stored in initialized data segment.
counter identifier will be stored in uninitialized data segment called ".bss".
one important thing about static variable is that it has internal linkage.so we can access this variable to a particular file.In which they are defined (not in other file).
We can access global variable glb_var in other file by using extern keyword. for eg:
//file2.c
output: 3 this is called external linkage.