When to use static keyword before global variables?

CStaticKeyword

C Problem Overview


Can someone explain when you're supposed to use the static keyword before global variables or constants defined in header files?

For example, lets say I have a header file with the line:

const float kGameSpriteWidth = 12.0f;

Should this have static in front of const or not? What are some best practices for using static?

C Solutions


Solution 1 - C

You should not define global variables in header files. You should define them in .c source file.

  • If global variable is to be visible within only one .c file, you should declare it static.

  • If global variable is to be used across multiple .c files, you should not declare it static. Instead you should declare it extern in header file included by all .c files that need it.

Example:

  • example.h

     extern int global_foo;
    
  • foo.c

     #include "example.h"
    
     int global_foo = 0;
     static int local_foo = 0;
    
     int foo_function()
     {
        /* sees: global_foo and local_foo
           cannot see: local_bar  */
        return 0;
     }
    
  • bar.c

     #include "example.h"
    
     static int local_bar = 0;
     static int local_foo = 0;
    
     int bar_function()
     {
         /* sees: global_foo, local_bar */
         /* sees also local_foo, but it's not the same local_foo as in foo.c
            it's another variable which happen to have the same name.
            this function cannot access local_foo defined in foo.c
         */
         return 0;
     }
    

Solution 2 - C

static renders variable local to the file which is generally a good thing, see for example this Wikipedia entry.

Solution 3 - C

Yes, use static

Always use static in .c files unless you need to reference the object from a different .c module.

Never use static in .h files, because you will create a different object every time it is included.

Solution 4 - C

Rule of thumb for header files:

  • declare the variable as extern int foo; and put a corresponding intialization in a single source file to get a modifiable value shared across translation units
  • use static const int foo = 42; to get a constant which can be inlined

Solution 5 - C

The static keyword is used in C to restrict the visibility of a function or variable to its translation unit. Translation unit is the ultimate input to a C compiler from which an object file is generated.

Check this: Linkage | Translation unit

Solution 6 - C

static before a global variable means that this variable is not accessible from outside the compilation module where it is defined.

E.g. imagine that you want to access a variable in another module:

foo.c

int var; // a global variable that can be accessed from another module
// static int var; means that var is local to the module only.
...

bar.c

extern int var; // use the variable in foo.c
...

Now if you declare var to be static you can't access it from anywhere but the module where foo.c is compiled into.

Note, that a module is the current source file, plus all included files. i.e. you have to compile those files separately, then link them together.

Solution 7 - C

The correct mechanism for C++ in anonymous namespaces. If you want something that is local to your file, you should use an anonymous namespace rather than the static modifier.

Solution 8 - C

global static variables are initialized at compile-time unlike automatic

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMattView Question on Stackoverflow
Solution 1 - CTomek SzpakowiczView Answer on Stackoverflow
Solution 2 - CDirk EddelbuettelView Answer on Stackoverflow
Solution 3 - CDigitalRossView Answer on Stackoverflow
Solution 4 - CChristophView Answer on Stackoverflow
Solution 5 - CParagView Answer on Stackoverflow
Solution 6 - CKhaled AlshayaView Answer on Stackoverflow
Solution 7 - CrmnView Answer on Stackoverflow
Solution 8 - CCaptain ComicView Answer on Stackoverflow