How do I share variables between different .c files?

c

Solution

In fileA.c:

int myGlobal = 0;

In fileA.h

extern int myGlobal;

In fileB.c:

#include "fileA.h"
myGlobal = 1;

So this is how it works:

- the variable lives in fileA.c

- fileA.h tells the world that it exists, and what its type is (`int`)

- fileB.c includes fileA.h so that the compiler knows about myGlobal before fileB.c tries to use it.

Problem

How can I use a variable in one `.c` file when it has been previously defined in another `.c` file?

Original source

Related problems