Why and when to use static structures in C programming?

The static keyword in C has several effects, depending on the context it's applied to.

  • when applied to a variable declared inside a function, the value of that variable will be preserved between function calls.
  • when applied to a variable declared outside a function, or to a function, the visibility of that variable or function is limited to the "translation unit" it's declared in - ie the file itself. For variables this boils down to a kind of "locally visible global variable".

Both usages are pretty common in relatively low-level code like drivers.

The former, and the latter when applied to variables, allow functions to retain a notion of state between calls, which can be very useful, but this can also cause all kinds of nasty problems when the code is being used in any context where it is being used concurrently, either by multiple threads or by multiple callers. If you cannot guarantee that the code will strictly be called in sequence by one "user", you can pass a kind of "context" structure that's being maintained by the caller on each call.

The latter, applied to functions, allows a programmer to make the function invisible from outside of the module, and it MAY be somewhat faster with some compilers for certain architectures because the compiler knows it doesn't have to make the variable/function available outside the module - allowing the function to be inlined for example.


Something that apparently all other answers seem to miss: static is and specifies also a storage duration for an object, along with automatic (local variables) and allocated (memory returned by malloc and friends).

Objects with static storage duration are initialized before main() starts, either with the initializer specified, or, if none was given, as if 0 had been assigned to it (for structs and arrays this goes for each member and recursively).

The second property static sets for an identifier, is its linkage, which is a concept used at link time and tells the linker which identifiers refer to the same object. The static keyword makes an identifier have internal linkage, which means it cannot refer to identifiers of the same name in another translation unit.

And to be pedantic about all the sloppy answers I've read before: a static variable can not be referenced everyhere in the file it is declared. Its scope is only from its declaration (which can be between function definitions) to the end of the source file--or even smaller, to the end of the enclosing block.


struct variable

For a struct variable like static struct S s;, this has been widely discussed at: What does "static" mean in C?

struct definition: no effect:

static struct S { int i; int j; };

is the exact same as:

struct S { int i; int j; };

so never use it. GCC 4.8 raises a warning if you do it.

This is because struct definitions have no storage, and do no generate symbols in object files like variables and functions. Just try compiling and decompiling:

struct S { int i; int j; };
int i;

with:

gcc -c main.c
nm main.o

and you will see that there is no S symbol, but there is an i symbol.

The compiler simply uses definitions to calculate the offset of fields at compile time.

This is struct definitions are usually included in headers: they won't generate multiple separate data, even if included multiple times.

The same goes for enum.

C++ struct definition: deprecated in C++11

C++11 N3337 standard draft Annex C 7.1.1:

Change: In C ++, the static or extern specifiers can only be applied to names of objects or functions Using these specifiers with type declarations is illegal in C ++. In C, these specifiers are ignored when used on type declarations.

See also: https://stackoverflow.com/a/31201984/895245


If you declare a variable as being static, it is visible only in that translation unit (if globally declared) or retains its value from call to call (if declared inside a function).

In your case I guess it is the first case. In that case, probably the programmer didn't want the structure to be visible from other files.


The static modifier for the struct limits the scope of visibility of the structure to the current translation unit (i.e. the file).

NOTE: This answer assumes (as other responders have indicated) that your declaration is not within a function.