..
#gamedev

Differences in Static Keyword in C/C++

The static keyword can mean three differents things, depend on where you put it:

  1. global variable by transalation unit;
  2. internal access for functions;
  3. local persists for variable scope;

1 - global

If you put static for a varibable outside the functions (file scope), that variable can be touch for every functions, but not for another translation unit.

// a.cpp
static int foo;

int main()
{
        printf("foo was %d\n", foo);
        foo++;
        printf("foo is %d\n", foo);
}

// b.cpp
extern int foo; // ERROR: cannot be touch because static in a.cpp

The example above, if we remove the static keyword, we can access the variable foo globally by extern.

In the other words, static make our variable internal by transalation unit AND "global" if we use only one transalation unit for the project.

2 - internal

As I said before, if a variable was marked with static it becomes internal.

So if we put this in a function, the functions becomes internal as well. I mean, we only can call this function inside the transalation unit.

But it can still called by pointer.

// a.cpp

#include "test.h"

int main()
{
        test(); // ERROR: cannot call test because it is outside the transalation unit
}

// b.cpp

static void test()
{
}

Let's see the example using pointer:

// a.cpp
#include "test.h"

static void print()
{
        printf("Hello world\n");
}

int main()
{
        test(print); // WORK: We can pass a pointer, even it's static marked
}
// test.h
typedef void my_func();

void test(my_func *func);
// b.cpp
#include "test.h"

void test(my_func *func)
{
        func();
}

3 - Local persist

The same as global variable (value will persist during the program), however, by block scope.

In other words, if you change the value of variable marked as static inside a block function, that value persists. It's not freed by stack.

It's useful for debugging purpose.

static void print()
{
        static int i;
        printf("Hello world %d\n", i);
        i++;
}

int main()
{
        print();
        print();
        print();
        print();
}

/* OUTPUT
   Hello world 0
   Hello world 1
   Hello world 2
   Hello world 3
*/

Extra

For last but not least, the static keyword initialize all the values with zero. So, if you have a integer or struct, that values will be zero.