Skip navigation

Understanding how to use extern variables in C/C++

Start with a visual studio project.

create 5 files, and copy/paste the contents as show below in them.

Alternatively, esnips code package! (thanks esnips!)

externVarDeclaration.h

#ifndef EXTERN_VAR_DECL_H
#define EXTERN_VAR_DECL_H

extern int i ;  // This is like a "function prototype", only
// for variables.

// The extern declaration says, "this variable doesn't exist yet,
// but it WILL exist, and its value is going to be defined
// in another file that will be part of this project when it compiles."
// (see externVarDefinition.cpp)

// So, using an EXTERN is how you share a single global variable
// ACROSS MULTIPLE FILES.
// Call it a SUPERGLOBAL.  TO understand this, think about
// functions and function prototypes.

// Defining a global function, you kind of expect it to have
// "superglobal" status..
// like, if you define a function somewhere, you should be
// able to break it apart into some prototypes and #include
// those prototypes in ANY FILE that you want
// to use those functions.  Right?

// So, the reason you have prototypes is so that multiple
// files can #include the same prototypes (function
// DECLARATIONS) then the actual code for the
// DEFINITION of the functions is in one place,
// a .cpp file (like "function_set.cpp").  This way you don't
// have multiple re-definitions of the functions declared in
// function_set.h, but you may have multiple re-declarations
// (the compiler might see the same prototype several times
// in a row, which is fine, as long as it sees the BODY
// definition only once.)

// So, an extern'd variable is a lot like that.
// You basically treat it like a function prototype, where the

///// extern int i ;

// part is the DECLARATION (like a function prototype),
// and the part in externVarDefinition.cpp:

// int i = 500 ;

// is like the DEFINITION, or function body.

// C++ can only come across the DEFINITION (value giving part) __ONCE__ for any
// given variable.

// So with externs, you can #include the "extern variable declaration" into as
// many files as you like (just like you can #include the function prototypes
// into as many different files as you wish), so long as
// the "DEFINITION" (the int i = 500 part) only occurs once, in one file.

// If you uncomment this line below, you will see
// its ILLEGAL.

//////////int j = 50 ; ////ILLEGAL

// Surprised?  Well, very illegal!

// Reason:  EVEN WITH the #ifndef #include guards
// on this file.. because this "externVarDeclaration.h" file is
// #included in more than one other file, for some reason the C++
// LINKER (__NOT__ the compiler) will flag it as an error:

// "int j" already defined in main.obj.

// One day I'd like to be able to fully understand why this is
// myself, but for now, "EXTERN solves this problem".

#endif //EXTERN_VAR_DECL_H

function_set.h

#ifndef FUNCTION_SET_H
#define FUNCTION_SET_H

#include <stdio.h>
#include "externVarDeclaration.h"

void print() ;
void print2() ;
void print3() ;
void changeI() ;

#endif //FUNCTION_SET_H

externVarDefinition.cpp

#include "externVarDeclaration.h"

int i = 500 ;

// HERE we define the VALUE of the extern'd variable i.

function_set.cpp

#include "function_set.h"

void print()
{
  printf("func1 %d\n", i);
}

void print2()
{
  printf("func2 %d\n", i);
}

void print3()
{
  printf("func3 %d\n", i);
}

void changeI()
{
  // i is shared across the whole project
  i = 2000402;
}

main.cpp

#include <stdio.h>

#include "externVarDeclaration.h"
#include "function_set.h"


int main()
{
  printf( "The extern'd var is %d\n", i ) ;

  i = 20 ;
  print();

  i = 333 ;
  print2();

  changeI();

  printf("My oh my!  the extern'd var has changed to %d\n", i ) ;
}

About these ads

15 Comments

    • Meysam Sabahi
    • Posted July 27, 2009 at 8:59 am
    • Permalink

    Thanks a lot for your useful codes.

    Regards,
    Meysam

    • JUNO
    • Posted August 11, 2009 at 8:06 am
    • Permalink

    Very Thanx for ur posting

    • Kosta
    • Posted August 25, 2009 at 12:08 pm
    • Permalink

    Thank you for this article, now I understand it. :)

    • Kosta
    • Posted August 25, 2009 at 9:03 pm
    • Permalink

    Take a look at this :

    http://www.cplusplus.com/forum/general/13759/

    You’ll understand why the guards are not always working.

    Regards,
    Kosta

  1. I was wondering if I can share a variable value between tow modules.

    • ronie.gadiane@gmail.com
    • Posted May 27, 2011 at 2:08 am
    • Permalink

    nice explanation of EXTERN. tenks..

    • Eva
    • Posted June 10, 2011 at 9:07 am
    • Permalink

    Thank you for nice explanation of externs

    • Eva
    • Posted June 10, 2011 at 9:25 am
    • Permalink

    Asante sana kwa maelezo yako mazuri mno, ubarikiwe

    • jm
    • Posted December 10, 2011 at 4:29 am
    • Permalink

    nice :)

    • Josh
    • Posted March 9, 2012 at 12:51 pm
    • Permalink

    This is a very good explanation of how to use externs. Thankyou very much for taking the time to post a nice useful explanation with such a thorough working example. This really helped me out!

    • Anonymous
    • Posted March 12, 2012 at 9:25 am
    • Permalink

    I just want to say thank you for explaining one very good use of ‘extern’.

    It was valuable as a quick fix for global variables depended on by multiple files. Others told me that globals are bad in large projects, but this turned out to be a real solution. Thanks again.

  2. good!

    • nom nom cookies
    • Posted February 7, 2013 at 7:27 pm
    • Permalink

    Just a friendly suggestion. It can be hard for some people to read dark-grey on a black background.

  3. @nom nom cookies Your email address was hilarious.

    I’ve wanted to change the theme, but it would mean revising all my posts..

    • Anonymous
    • Posted June 20, 2013 at 6:50 am
    • Permalink

    thank you very much, great explained


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 43 other followers

%d bloggers like this: