[ACCEPTED]-static - used only for limiting scope?-scope
The static
keyword serves two distinct purposes 22 in C, what I call duration (the lifetime 21 of an object) and visibility (where you 20 can use an object from). Keep in mind the 19 C standard actually uses different words 18 for these two concepts but I've found in 17 teaching the language that it's best to 16 use everyday terms to begin with.
When used 15 at file level (outside of any function), it 14 controls visibility. The duration of variables 13 defined at file level are already defined 12 as being the entire duration of the program 11 so you don't need static
for that.
Static variables 10 at file level are invisible to anything 9 outside the translation unit (the linker 8 can't see it).
When used at function level 7 (inside a function), it controls duration. That's 6 because the visibility is already defined 5 as being local to that function.
In that 4 case, the duration of the variable is the 3 entire duration of the program and the value 2 is maintained between invocations of the 1 function.
You are misusing the term "scope". static
in C 20 has absolutely nothing to do with scope.
Scope is 19 the region where the name of an entity (variable, function, typename 18 etc.) is visible. In C language "file scope" is 17 the largest scope ever. For that reason, there's 16 no point in limiting anything to a single 15 file: there's simply nothing larger to limit. There's 14 no such thing as "global scope" in C. The 13 term "global scope" is sometimes used informally, but 12 in that case it has the same meaning as 11 "file scope".
Again, static
in C has absolutely 10 nothing to do with scope. static
in C affects storage duration of an 9 object and linkage of an identifier. When used 8 with objects (variables) static
gives the object 7 static storage duration (i.e. the object exists as long as the 6 program runs). And, when used with identifiers 5 of non-local objects or functions, it gives 4 them internal linkage, meaning that the same identifier 3 refers to the same entity within a single 2 translation unit (where the entity is defined), but 1 not in other translation units.
static
is also used within a function definition 4 to define a variable which keeps its value 3 between function calls. I found an example here. In contrast, variables 2 which are created anew with each function 1 call are called automatic.
An example to augment Kinopiko’s answer:
#include <stdio.h>
int foo() {
static int foo = 0;
return ++foo;
}
int main() {
printf("%i\n", foo()); // 1
printf("%i\n", foo()); // 2
}
This 4 can be used for example to return a safe 3 pointer to a local function variable. Or 2 in Objective-C it’s sometimes used to guard 1 against repeated class initialization:
- (void) initialize
{
static BOOL initialized = NO;
if (initialized)
return;
// …perform initialization…
initialized = YES;
}
You are correct, this is called "static 9 linkage": The symbol declared as static
is only 8 available in the compilation unit where 7 it is defined.
The other use of static
would be 6 inside a function:
void f() {
static int counter = 0;
counter++;
// ...
}
In this case the variable 5 is only initialized once and keeps it's 4 value through different calls of that function, like 3 it would be a global variable. In this example 2 the counter
variable counts the number of times 1 the function was called.
A variable may have three kinds of storage:
- In program's Static Area
- On stack (during function call)
- On Heap (when you allocate using new/malloc)
Global 9 variables are always stored in static area. But 8 to store a local variable in static area, you 7 need the keyword static
. As a static variable 6 is not allocated on stack, you can access 5 the variable on subsequent calls.
Also static
keyword 4 at global scope gives a variable internal 3 linkage.Consequently the variable cannot 2 be accessed from some other file using the 1 extern
qualifier.
internal linkage vs external linkage by 14 example
//file1.c
#include <stdio.h>
int glb_var=3;//global variable
int func(); //prototype of function
int main()
{
func();
func();
func();
return 0;
}
int func()
{
static int counter=0;//static varible
printf("val of counter=%d",counter);
counter+=5;
return 0;
}
when we will compile this 13 program and run this program then os will 12 load this program in memory.then below things 11 will happened:
glb_var identifier will be 10 stored in initialized data segment.
counter 9 identifier will be stored in uninitialized 8 data segment called ".bss".
- static variable initialized once and the values persists during function calls.because static variable is stored in data segment not in stack so static variable persist during function calls. So output of the program will be: 0 5 10
one important 7 thing about static variable is that it has 6 internal linkage.so we can access this variable 5 to a particular file.In which they are defined 4 (not in other file).
We can access global 3 variable glb_var in other file by using 2 extern keyword. for eg:
//file2.c
#include <stdio.h>
extern glb_var; //for declaration of this variable
int main()
{
if(glb_var)
{
printf("glb_var=%d",glb_var);
}
}
output: 3 this 1 is called external linkage.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.