What does “static” mean in C?

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

    In C, static has two meanings, depending on scope of its use. In the global scope, when an object is declared at the file level, it means that that object is only visible within that file.

    At any other scope it declares an object that will retain its value between the different times that the particular scope is entered. For example, if an int is delcared within a procedure:

    void procedure(void)
    {
       static int i = 0;
    
       i++;
    }
    

    the value of 'i' is initialized to zero on the first call to the procedure, and the value is retained each subsequent time the procedure is called. if 'i' were printed it would output a sequence of 0, 1, 2, 3, ...

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

    If you declare this in a mytest.c file:

    static int my_variable;
    

    Then this variable can only be seen from this file. The variable cannot be exported anywhere else.

    If you declare inside a function the value of the variable will keep its value each time the function is called.

    A static function cannot be exported from outside the file. So in a *.c file, you are hiding the functions and the variables if you declare them static.

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

    static means different things in different contexts.

    1. You can declare a static variable in a C function. This variable is only visible in the function however it behaves like a global in that it is only initialized once and it retains its value. In this example, everytime you call foo() it will print an increasing number. The static variable is initialized only once.

      void foo ()
      {
      static int i = 0;
      printf("%d", i); i++
      }
      
    2. Another use of static is when you implement a function or global variable in a .c file but don't want its symbol to be visible outside of the .obj generated by the file. e.g.

      static void foo() { ... }
      
    0 讨论(0)
  • 2020-11-21 05:44

    I hate to answer an old question, but I don't think anybody has mentioned how K&R explain it in section A4.1 of "The C Programming Language".

    In short, the word static is used with two meanings:

    1. Static is one of the two storage classes (the other being automatic). A static object keeps its value between invocations. The objects declared outside all blocks are always static and cannot be made automatic.
    2. But, when the static keyword (big emphasis on it being used in code as a keyword) is used with a declaration, it gives that object internal linkage so it can only be used within that translation unit. But if the keyword is used in a function, it changes the storage class of the object (the object would only be visible within that function anyway). The opposite of static is the extern keyword, which gives an object external linkage.

    Peter Van Der Linden gives these two meanings in "Expert C Programming":

    • Inside a function, retains its value between calls.
    • At the function level, visible only in this file.
    0 讨论(0)
  • 2020-11-21 05:45

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

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

    Static variables have a property of preserving their value even after they are out of their scope!Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope.

    Look at this for example - A static int variable remains in memory while the program is running. A normal or auto variable is destroyed when a function call where the variable was declared is over.

    #include<stdio.h> 
    int fun() 
    { 
      static int count = 0; 
      count++; 
      return count; 
    } 
    
    int main() 
    { 
      printf("%d ", fun()); 
      printf("%d ", fun()); 
      return 0; 
    }
    

    This will output: 1 2

    As 1 stays in the memory as it was declared static

    Static variables (like global variables) are initialized as 0 if not initialized explicitly. For example in the below program, value of x is printed as 0, while value of y is something garbage. See this for more details.

    #include <stdio.h> 
    int main() 
    { 
        static int x; 
        int y; 
        printf("%d \n %d", x, y); 
    }
    

    This will output : 0 [some_garbage_value]

    These are the major ones I found that weren't explained above for a newbie!

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