Search code examples
cinlinec99static-membersstatic-functions

C99 static inline function with static local variable


If I write something like this in a C99 header:

static inline void f()
{
    static int x = 0;
    // Do something with x
}

Is it guaranteed that each module including this header gets a separate instantiation of f() and its own instantiation of x?


Solution

  • Yes, by definition, that's what static means. The fact that it's in a header is irrelevant. Conceptually, the C preprocessor creates one file to compile per .c file as if you had just (re)typed all the text in the header into the .c yourself.