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
?
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
foo.c
bar.c
global static variables are initialized at compile-time unlike automatic