3

We can assume that the static keyword shall not be used in a header file - for variables.

A header file only contains extern declarations of variables — never static or unqualified variable definitions.

Is there any exception where the C static keyword should or could be used in header files for variables? Why?

Guillaume D
  • 2,202
  • 2
  • 10
  • 37
  • In the cases where you want an object or function to have internal linkage. – AnArrayOfFunctions Jun 21 '19 at 09:37
  • 2
    There are plenty of headers in Linux source code that contain `static` function definitions but from I understood you're only asking about variables. – Arkadiusz Drabczyk Jun 21 '19 at 09:45
  • The linked question is about sharing one global variable in different files. If you don't want it to be 1 shared variable, but instead an independent variable in each source file, things may be different. – Gerhardh Jun 21 '19 at 10:07
  • If you make a variable static in a header file, each compilation unit using the header will create its own instance of the variable. Trying hard, but failing to think up a scenario where such a setup would be useful... – Aconcagua Jun 21 '19 at 10:21

2 Answers2

4

I use static in header files only to define constants. Example:

MyProject.h

 static const int DebugLevel = 3;

Module.c

int foo(int x)
{
   if (DebugLevel>2) 
      printf("foo(int x) called with x=%d\n", x);
   ...
}

The advantages of this approach:

  • Changing one statement at a single location is automatically propagated in all modules.
  • Using const allows the compiler to optimize the if statement. The printf call might be removed too.
  • Using static allows the compiler to optimize the variable completely away, no memory is wasted.
user5329483
  • 1,260
  • 7
  • 11
  • How is this different/better than just using #define ? – Lee Daniel Crocker Jun 21 '19 at 16:36
  • @LeeDanielCrocker: My favourite source code formatter places all preprocessor directives at column 1 and destroys my intents and my code structure. Point two: I have the full scale of features of the C language available. Point three: My compiler kills me if I use undefined symbols. Yes, I use the preprocessor with conditional compilation too. But only if I cannot achieve a similar result with my compiler with its optimizations. – user5329483 Jun 21 '19 at 21:18
3

Translation-unit-specific (i.e., C source file) information that needs to be a part of every compiled object file, but needs to be different for each input source file for some reason.

For example, debugging or build information used to track the origin of a specific object file. Or maybe an organization wants to embed copyright information directly into each object file.

For example, a "buildinfo.h" file:

#ifndef BUILDINFO_H_INCLUDED
#define BUILDINFO_H_INCLUDED

static char my_org_copyright[] = "Copyright ...";
static char my_org_build_info[] = "Compiled on " __DATE__ "@" __TIME__;

#endif

The usefulness of such data is debatable, but I have seen such constructs used in code produced by a very large corporation...

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56