Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use static keyword before global variables?

Tags:

c

static

keyword

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?

like image 239
Matt Avatar asked Dec 06 '09 21:12

Matt


People also ask

Can we use static in place of global?

A static variable can be either a global or local variable. Both are created by preceding the variable declaration with the keyword static. A local static variable is a variable that can maintain its value from one function call to another and it will exist until the program ends.

Are global variables static by default in C?

Global and static variables are initialized to their default values because it is in the C or C++ standards and it is free to assign a value by zero at compile time. Both static and global variable behave same to the generated object code.

Whats is static keyword and it uses when declared as global variable?

Static is a keyword used in C programming language. It can be used with both variables and functions, i.e., we can declare a static variable and static function as well. An ordinary variable is limited to the scope in which it is defined, while the scope of the static variable is throughout the program.

What is difference between static global and non static global variable in C?

A global static variable is only available in the translation unit (i.e. source file) the variable is in. A non-static global variable can be referenced from other source files.


2 Answers

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; } 
like image 200
Tomek Szpakowicz Avatar answered Oct 13 '22 08:10

Tomek Szpakowicz


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

like image 23
Dirk Eddelbuettel Avatar answered Oct 13 '22 08:10

Dirk Eddelbuettel