Broad Network


The C++ extern Specifier

Specifiers in C++ - Part 3

Forward: In this part of the series, I explain the use of the extern specifier in C++.

By: Chrysanthus Date Published: 25 Aug 2012

Introduction

This is part 3 of my series, Specifiers in C++. In this part of the series, I explain the use of the extern specifier in C++. This specifier is one of the storage class specifiers. I assume you have read the previous part of the series. This is a continuation.

Functions of the extern Specifier
The extern specifier is used to precise that an identifier in an inner block (scope) is the same as the one declared in the outer block (scope).
It enables the declared identifier of the outer block, to be typed below the same identifier with the extern specifier of the inner block.
It enables an identifier declared in a different file to be seen and used in the main file (accessing same object in a different file).

I elaborate on these functions below:

Making Identifier in Outer and Inner Blocks Same
Consider the following program:

#include <iostream>
using namespace std;

int myInt = 33;

int main()
    {
        cout << myInt << "\n";        

        int myInt = 44;

        cout << myInt;
    
        return 0;
    }

The identifier, myInt, declared (and initialized) outside the function block is different from the identifier of the same name, declared (and initialized) inside the function block. You can try the code just to be sure. So the two identifiers with the same name, but in the outer and inner block, are (represent) different objects.

This kind of coding is correct, if you really know what you are doing. However, you should avoid it. If you want to precise that the identifier outside and the one inside are the same object, then you have to use the “extern” specifier for the one inside. The following code illustrates this:

#include <iostream>
using namespace std;

int myInt = 33;

int main()
    {
        cout << myInt << "\n";        

        extern int myInt;

        cout << myInt;
    
        return 0;
    }

The identifier inside is preceded by the specifier, extern, and of course its simple type specifier, int, as well. The identifier inside, should not be initialized.

So, to precise that two identifiers with the same name, one inside a block and one declared outside the block, are the same object, use the extern specifier, for the declaration without initialization, for the one inside the block.

Declaration after Application
In normal C++ execution of a program, execution moves from one statement to the next, beginning from the very top of the file to the very bottom. In this case, if you type the application of an identifier before you declare it, the program will not work. If you want the program to work under this typing condition, then you have to use the specifier, extern, and declare the identifier in an inner block (scope), and then you re-declare the identifier, outside the block, below the block, without the specifier, extern. The application of the identifier takes place in the inner block, after the typed declaration with, extern. The following program illustrates this:

#include <iostream>
using namespace std;

int main()
    {
        extern int myInt;

        cout << myInt;

        return 0;
    }


int myInt = 55;

Note in the code that the declaration without extern, is typed below the main function. So, the official declaration has been typed below the application, thanks to the extern specifier in the block.

Accessing Same Object Declared in a Different File
Type only the following statement is a file. Name the file, “demo.cpp” and save it in the working directory.

    int fineInt = 18;

Type the following program in a new file. Name it, “mainfile.cpp” and save it still in the working directory.

#include <iostream>
using namespace std;

int main()
    {
        extern int fineInt;
        cout << fineInt;
    
        return 0;
    }

In the “demo.cpp” file, an object has been declared and initialized in the normal way. In the main file, the object has been re-declared in a block, with the, extern specifier, but without initialization. Under this condition, the same object is seen in both files, identified by the same name.

Compile the two files together with the following command (MinGW compiler):

    g++ mainfile.cpp demo.cpp -o complete.exe

The resulting single executable file, has been called, “complete.exe” in the command. Note in the command, that you have a minus sign just before o and not some long dash that your system (computer) may give you.

To test the program, type the name, complete.exe, in the working directory prompt and press Enter.

I have explained three functions of the, extern specifier. So, we can stop here for this part of the series. We continue in the next part.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message