What does “static” mean in C?

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

提交回复
热议问题