What does “static” mean in C?

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

提交回复
热议问题