What does “static” mean in C?

后端 未结 19 1902
误落风尘
误落风尘 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:19

    There are 2 cases:

    (1) Local variables declared static: Allocated in data segment instead of stack. Its value retains when you call the function again.

    (2) Global variables or functions declared static: Invisible outside compilation unit (i.e. are local symbols in symbol table during linking).

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

    There is one more use not covered here, and that is as part of an array type declaration as an argument to a function:

    int someFunction(char arg[static 10])
    {
        ...
    }
    

    In this context, this specifies that arguments passed to this function must be an array of type char with at least 10 elements in it. For more info see my question here.

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

    If you declare a variable in a function static, its value will not be stored on the function call stack and will still be available when you call the function again.

    If you declare a global variable static, its scope will be restricted to within the file in which you declared it. This is slightly safer than a regular global which can be read and modified throughout your entire program.

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

    A static variable is a special variable that you can use in a function, and it saves the data between calls, and it does not delete it between calls. For example:

    void func(){
        static int count; // If you don't declare its value, the value automatically initializes to zero
        printf("%d, ", count);
        ++count;
    }
    
    void main(){
        while(true){
            func();
        }
    }
    

    The output:

    0, 1, 2, 3, 4, 5, ...

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

    Multi-file variable scope example

    Here I illustrate how static affects the scope of function definitions across multiple files.

    a.c

    #include <stdio.h>
    
    /*
    Undefined behavior: already defined in main.
    Binutils 2.24 gives an error and refuses to link.
    https://stackoverflow.com/questions/27667277/why-does-borland-compile-with-multiple-definitions-of-same-object-in-different-c
    */
    /*int i = 0;*/
    
    /* Works in GCC as an extension: https://stackoverflow.com/a/3692486/895245 */
    /*int i;*/
    
    /* OK: extern. Will use the one in main. */
    extern int i;
    
    /* OK: only visible to this file. */
    static int si = 0;
    
    void a() {
        i++;
        si++;
        puts("a()");
        printf("i = %d\n", i);
        printf("si = %d\n", si);
        puts("");
    }
    

    main.c

    #include <stdio.h>
    
    int i = 0;
    static int si = 0;
    
    void a();    
    
    void m() {
        i++;
        si++;
        puts("m()");
        printf("i = %d\n", i);
        printf("si = %d\n", si);
        puts("");
    }
    
    int main() {
        m();
        m();
        a();
        a();
        return 0;
    }
    

    GitHub upstream.

    Compile and run:

    gcc -c a.c -o a.o
    gcc -c main.c -o main.o
    gcc -o main main.o a.o
    

    Output:

    m()
    i = 1
    si = 1
    
    m()
    i = 2
    si = 2
    
    a()
    i = 3
    si = 1
    
    a()
    i = 4
    si = 2
    

    Interpretation

    • there are two separate variables for si, one for each file
    • there is a single shared variable for i

    As usual, the smaller the scope, the better, so always declare variables static if you can.

    In C programming, files are often used to represent "classes", and static variables represent private static members of the class.

    What standards say about it

    C99 N1256 draft 6.7.1 "Storage-class specifiers" says that static is a "storage-class specifier".

    6.2.2/3 "Linkages of identifiers" says static implies internal linkage:

    If the declaration of a file scope identifier for an object or a function contains the storage-class specifier static, the identifier has internal linkage.

    and 6.2.2/2 says that internal linkage behaves like in our example:

    In the set of translation units and libraries that constitutes an entire program, each declaration of a particular identifier with external linkage denotes the same object or function. Within one translation unit, each declaration of an identifier with internal linkage denotes the same object or function.

    where "translation unit is a source file after preprocessing.

    How GCC implements it for ELF (Linux)?

    With the STB_LOCAL binding.

    If we compile:

    int i = 0;
    static int si = 0;
    

    and disassemble the symbol table with:

    readelf -s main.o
    

    the output contains:

    Num:    Value          Size Type    Bind   Vis      Ndx Name
      5: 0000000000000004     4 OBJECT  LOCAL  DEFAULT    4 si
     10: 0000000000000000     4 OBJECT  GLOBAL DEFAULT    4 i
    

    so the binding is the only significant difference between them. Value is just their offset into the .bss section, so we expect it to differ.

    STB_LOCAL is documented on the ELF spec at http://www.sco.com/developers/gabi/2003-12-17/ch4.symtab.html:

    STB_LOCAL Local symbols are not visible outside the object file containing their definition. Local symbols of the same name may exist in multiple files without interfering with each other

    which makes it a perfect choice to represent static.

    Variables without static are STB_GLOBAL, and the spec says:

    When the link editor combines several relocatable object files, it does not allow multiple definitions of STB_GLOBAL symbols with the same name.

    which is coherent with the link errors on multiple non static definitions.

    If we crank up the optimization with -O3, the si symbol is removed entirely from the symbol table: it cannot be used from outside anyways. TODO why keep static variables on the symbol table at all when there is no optimization? Can they be used for anything? Maybe for debugging.

    See also

    • analogous for static functions: https://stackoverflow.com/a/30319812/895245
    • compare static with extern, which does "the opposite": How do I use extern to share variables between source files?

    C++ anonymous namespaces

    In C++, you might want to use anonymous namespaces instead of static, which achieves a similar effect, but further hides type definitions: Unnamed/anonymous namespaces vs. static functions

    0 讨论(0)
  • 2020-11-21 05:23
    1. A static variable inside a function keeps its value between invocations.
    2. A static global variable or a function is "seen" only in the file it's declared in

    (1) is the more foreign topic if you're a newbie, so here's an example:

    #include <stdio.h>
    
    void foo()
    {
        int a = 10;
        static int sa = 10;
    
        a += 5;
        sa += 5;
    
        printf("a = %d, sa = %d\n", a, sa);
    }
    
    
    int main()
    {
        int i;
    
        for (i = 0; i < 10; ++i)
            foo();
    }
    

    This prints:

    a = 15, sa = 15
    a = 15, sa = 20
    a = 15, sa = 25
    a = 15, sa = 30
    a = 15, sa = 35
    a = 15, sa = 40
    a = 15, sa = 45
    a = 15, sa = 50
    a = 15, sa = 55
    a = 15, sa = 60
    

    This is useful for cases where a function needs to keep some state between invocations, and you don't want to use global variables. Beware, however, this feature should be used very sparingly - it makes your code not thread-safe and harder to understand.

    (2) Is used widely as an "access control" feature. If you have a .c file implementing some functionality, it usually exposes only a few "public" functions to users. The rest of its functions should be made static, so that the user won't be able to access them. This is encapsulation, a good practice.

    Quoting Wikipedia:

    In the C programming language, static is used with global variables and functions to set their scope to the containing file. In local variables, static is used to store the variable in the statically allocated memory instead of the automatically allocated memory. While the language does not dictate the implementation of either type of memory, statically allocated memory is typically reserved in data segment of the program at compile time, while the automatically allocated memory is normally implemented as a transient call stack.

    And to answer your second question, it's not like in C#.

    In C++, however, static is also used to define class attributes (shared between all objects of the same class) and methods. In C there are no classes, so this feature is irrelevant.

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