Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Sharing variables between files of code in C - extern

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:
  int childcount;
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:

#ifdef GLOBALS
#define EXTERN
#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
before your
  #include "myglobals.h"
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 articles
C211 - C and C based languages - Standard Libraries and other headers
  [3148] OpenGL / C / C++ - an example to get you started - (2011-01-26)
  [3234] Your program - you just provide the filling in the sandwich - (2011-04-08)
  [3244] C and C++ - preprocess, compile, load, run - what each step is for - (2011-04-12)
  [4465] Sockets, time handling and keyboard interrupt handling in C - (2015-03-27)

C204 - C and C based languages - Functions, Macros and programs in multiple files
  [775] Do not duplicate your code - (2006-06-23)
  [1163] A better alternative to cutting and pasting code - (2007-04-26)
  [1478] Some new C programming examples - files, structs, unions etc - (2007-12-19)
  [2570] Function Prototypes in C - (2010-01-11)
  [2841] C Course exercise and sample answer - source in 2 files - (2010-06-30)
  [3237] Using functions to keep look and feel apart from calculations - simple C example - (2011-04-09)
  [3717] Returning extra results from a function in C - (2012-05-03)
  [3721] Naming blocks of code, structures and Object Orientation - efficient coding in manageable chunks - (2012-05-06)
  [4338] Passing arrays into functions in C - (2014-12-02)
  [4554] Passing information into functions in C - by name, by value - (2015-10-26)
  [4555] Preprocessor directives in C and C++ - what they mean - (2015-10-27)
  [4557] Function prototype - what they are and why you should use them - C and C++ - (2015-10-27)

Back to
Summary of Wiltshire Core Strategy responses
Previous and next
Horse's mouth home
Forward to
What does const mean? C and C++
Some other Articles
Creating, setting up and using objects in C++
Where are your objects stored in C++?
Complete teaching example - C++, inheritance, polymorphism
What does const mean? C and C++
Sharing variables between files of code in C - extern
Summary of Wiltshire Core Strategy responses
C Structs - what, how and why
The what and why of C pointers
Reading and writing files in C
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page

This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2575_Sha ... xtern.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb