Silicomancer Silicomancer - 12 days ago 5
C Question

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
?

Answer

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.