What does “static” mean in C?

后端 未结 19 1953
误落风尘
误落风尘 2020-11-21 05:18

I\'ve seen the word static used in different places in C code; is this like a static function/class in C# (where the implementation is shared across objects)?

相关标签:
19条回答
  • 2020-11-21 05:28

    Static variables in C have the lifetime of the program.

    If defined in a function, they have local scope, i.e. they can be accessed only inside those functions. The value of static variables is preserved between function calls.

    For example:

    void function()
    {
        static int var = 1;
        var++;
        printf("%d", var);
    }
    
    int main()
    {
        function(); // Call 1
        function(); // Call 2
    }
    

    In the above program, var is stored in the data segment. Its lifetime is the whole C program.

    After function call 1, var becomes 2. After function call 2, var becomes 3.

    The value of var is not destroyed between functions calls.

    If var had between non static and local variable, it would be stored in the stack segment in the C program. Since the stack frame of the function is destroyed after the function returns, the value of var is also destroyed.

    Initialized static variables are stored in the data segment of the C program whereas uninitialized ones are stored in the BSS segment.

    Another information about static: If a variable is global and static, it has the life time of the C program, but it has file scope. It is visible only in that file.

    To try this:

    file1.c

    static int x;
    
    int main()
    {
        printf("Accessing in same file%d", x):
    }
    

    file2.c

        extern int x;
        func()
        {
            printf("accessing in different file %d",x); // Not allowed, x has the file scope of file1.c
        }
    
    run gcc -c file1.c
    
    gcc -c file2.c
    

    Now try to link them using:

    gcc -o output file1.o file2.o
    

    It would give a linker error as x has the file scope of file1.c and the linker would not be able to resolve the reference to variable x used in file2.c.

    References:

    1. http://en.wikipedia.org/wiki/Translation_unit_(programming)
    2. http://en.wikipedia.org/wiki/Call_stack
    0 讨论(0)
  • 2020-11-21 05:31

    It depends:

    int foo()
    {
       static int x;
       return ++x;
    }
    

    The function would return 1, 2, 3, etc. --- the variable is not on the stack.

    a.c:

    static int foo()
    {
    }
    

    It means that this function has scope only in this file. So a.c and b.c can have different foo()s, and foo is not exposed to shared objects. So if you defined foo in a.c you couldn't access it from b.c or from any other places.

    In most C libraries all "private" functions are static and most "public" are not.

    0 讨论(0)
  • 2020-11-21 05:34

    It is important to note that static variables in functions get initialized at the first entry into that function and persist even after their call has been finished; in case of recursive functions the static variable gets initialized only once and persists as well over all recursive calls and even after the call of the function has been finished.

    If the variable has been created outside a function, it means that the programmer is only able to use the variable in the source-file the variable has been declared.

    0 讨论(0)
  • 2020-11-21 05:35

    People keep saying that 'static' in C has two meanings. I offer an alternate way of viewing it that gives it a single meaning:

    • Applying 'static' to an item forces that item to have two properties: (a) It is not visible outside the current scope; (b) It is persistent.

    The reason it seems to have two meanings is that, in C, every item to which 'static' may be applied already has one of these two properties, so it seems as if that particular usage only involves the other.

    For example, consider variables. Variables declared outside of functions already have persistence (in the data segment), so applying 'static' can only make them not visible outside the current scope (compilation unit). Contrariwise, variables declared inside of functions already have non-visibility outside the current scope (function), so applying 'static' can only make them persistent.

    Applying 'static' to functions is just like applying it to global variables - code is necessarily persistent (at least within the language), so only visibility can be altered.

    NOTE: These comments only apply to C. In C++, applying 'static' to class methods is truly giving the keyword a different meaning. Similarly for the C99 array-argument extension.

    0 讨论(0)
  • 2020-11-21 05:37

    Short answer ... it depends.

    1. Static defined local variables do not lose their value between function calls. In other words they are global variables, but scoped to the local function they are defined in.

    2. Static global variables are not visible outside of the C file they are defined in.

    3. Static functions are not visible outside of the C file they are defined in.

    0 讨论(0)
  • 2020-11-21 05:38

    A static variable value persists between different function calls andits scope is limited to the local block a static var always initializes with value 0

    0 讨论(0)
提交回复
热议问题