In C, If you want to share a variable between a number of functions, you can declare it at the top of your file, outside the functions. It's then an element that's held in memory all the time and available to all your functions. Since your functions are separate elements which are joined together at load time (when you make up the executable file), the variable you're sharing also has to be know to the loaded - it becomes one of the names (symbols) that it needs to load / allocate a position to, just like each of the functions is.
Should you use the same name (i.e. same named variable) in multiple source files, you will be sharing
that variable across between them (In other languages, this is often called - a poor name
- a global.
With compilers such as gcc (The Gnu Compiler Collection), the occurrence of the same named and shared variable in multiple source files doesn't present the loader with a problem, but with some other compilers it does - that's because the loader has two definitions of the same variable. It's the same problem that you see when you get "double definition" messages from the loaded when a function appears in two different source files. And the solution is to add the keyword extern
in front of the declaration in all but one of the files. Thus:
in the one set of source where you want the variable to be located and:
extern int childcount;
in every other set of source where you want in to be used (but not actually located) - the word "extern" is short for external
which meand that the sybol is known about, but external to the blocks of code in the current object file.
That's good ... it works well ... but if you're coding a major application, you need to manage it - otherwise you can have a major administration job as you maintain and upgrade your program.
The scheme that's usually followed is to place all the variable that you want to share between a suite of source files into a single header file, and then to use the C preprocessor to prepend each declaration with the word extern
unless it's the single case where you want the variables to be physically located in memory.
Here's a sample header file declaring shared variables across an application's C source files:
#define EXTERN extern
EXTERN int nbeasts;
EXTERN float hippos;
And in one (and ONLY one of your source files, you'll add the line:
#define GLOBALS 1
or whatever (better) name you have chosen for the file.
There's a complete example from the C Course I have just completed
on our web site ... the included file with its preprocessor headers is [here]
, the file of functions with which the variables are loaded is [here]
, and the sample file (which could be one of many) that uses those variables is [here]
An element of caution. You should use parameters to pass variables around between your functions most of the time. Shared variables as described in this article do have their place - typically when you're using data that's referenced by a whole series of closeknit routines - but I do not recommend them as the normal way of transferring data back and forth! (written 2010-01-14, updated 2010-01-15)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesC204 - C and C based languages - Functions, Macros and programs in multiple files 
Function prototype - what they are and why you should use them - C and C++ - (2015-10-27) 
Preprocessor directives in C and C++ - what they mean - (2015-10-27) 
Passing information into functions in C - by name, by value - (2015-10-26) 
Passing arrays into functions in C - (2014-12-02) 
Naming blocks of code, structures and Object Orientation - efficient coding in manageable chunks - (2012-05-06) 
Returning extra results from a function in C - (2012-05-03) 
Using functions to keep look and feel apart from calculations - simple C example - (2011-04-09) 
C Course exercise and sample answer - source in 2 files - (2010-06-30) 
Function Prototypes in C - (2010-01-11) 
Some new C programming examples - files, structs, unions etc - (2007-12-19) 
A better alternative to cutting and pasting code - (2007-04-26) 
Do not duplicate your code - (2006-06-23)C211 - C and C based languages - Standard Libraries and other headers 
Sockets, time handling and keyboard interrupt handling in C - (2015-03-27) 
C and C++ - preprocess, compile, load, run - what each step is for - (2011-04-12) 
Your program - you just provide the filling in the sandwich - (2011-04-08) 
OpenGL / C / C++ - an example to get you started - (2011-01-26)
Some other Articles
Creating, setting up and using objects in C++Where are your objects stored in C++?Complete teaching example - C++, inheritance, polymorphismWhat does const mean? C and C++Sharing variables between files of code in C - externSummary of Wiltshire Core Strategy responsesC Structs - what, how and whyThe what and why of C pointersReading and writing files in C