static - used only for limiting scope?

2019-04-07 11:06发布

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?)

标签: c static scope
7条回答
Summer. ? 凉城
2楼-- · 2019-04-07 11:43

A variable may have three kinds of storage:

  1. In program's Static Area
  2. On stack (during function call)
  3. On Heap (when you allocate using new/malloc)

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 the extern qualifier.

查看更多
乱世女痞
3楼-- · 2019-04-07 11:47

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.

查看更多
我只想做你的唯一
4楼-- · 2019-04-07 11:49

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.

查看更多
闹够了就滚
5楼-- · 2019-04-07 11:49

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:

void f() {
  static int counter = 0;
  counter++;
  // ...
}

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.

查看更多
叼着烟拽天下
6楼-- · 2019-04-07 11:54

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.

查看更多
叛逆
7楼-- · 2019-04-07 11:55

internal linkage vs external linkage by 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 program and run this program then os will load this program in memory.then below things will happened:

  1. glb_var identifier will be stored in initialized data segment.

  2. counter identifier will be stored in uninitialized data segment called ".bss".

  3. 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 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

#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 is called external linkage.

查看更多
登录 后发表回答