user2914275
user2914275

Reputation: 43

Extern for Global and static global variables

I am a beginner for c++ i was going through some code where i read that global variables persist till the end of our program and static global variables will have scope till the end of that file.Here is an example program where am trying to access both global and static global variables in another file.

Could anyone please explain how is it possible to use extern for static global variable in c++?

If i have a header file with both global and static global variables and i include it in my source file ans use extern for both and print values its printing

If i do so it wont give any error or warning. program will run and both values are displayed in source file

Header file header.h

int varGlobal;
static int staticVarGlobal = 10

Source file

#include<iostream>
#include "header.h"

extern int varGlobal;
extern int staticVarGlobal;

using namespace std;

int main()
{
cout<<"Global variable : "<<varGlobal<<endl;
cout<<"Static Global variable : "<<++staticVarGlobal<<endl;
}
Output

Global variable : 0
Static Global variable : 11

So how does this work?

Upvotes: 2

Views: 8696

Answers (3)

Ayushi Jha
Ayushi Jha

Reputation: 4023

Here's a link you might find useful : extern, global variables etc The keyword extern if used for a global variable means that it will be accessible from another file, whereas static variable has file scope - it cannot be accessed from outside the file.

It is possible to create a global variable in one file and access it from another file. In order to do this, the variable must be declared in both files, but the keyword extern must precede the "second" declaration.

A global static variable is one that can only be accessed in the file where it is created. This variable is said to have file scope.

Here's another question from stack overflow, you may get help from the answers posted there.

When you declare a variable as static, you are restricting it to the current source file. If you declare it as extern, you are saying that the variable exists, but are declared somewhere else, and if you don't have it declared elsewhere (without the extern keyword) you will get a link error (symbol not found).

Static restricts scope of variable to the same file and extern allows variable (global) to be accessed from another file, it is not possible to use extern for static global variable in c++.

Upvotes: 0

Mohit Jain
Mohit Jain

Reputation: 30489

Could anyone please explain how is it possible to use extern for static global variable in c++?

No, it is not possible. For more details, please continue reading.

You can not use static global in other file even if you use extern (If I rephrase extern and static are conflicting specifiers)

But life time of static global is throughout the life of program.

The only way to use static in different compilation unit (source file) is to pass the address of this static variable to other file and use it by dereferencing the pointer.

If i have a header file with both global and static global variables and i include it in my source file ans use extern for both and print values its printing

static global This works because difference instances of variable is created for each source file. So if you change the value of variable in one of the file, it won't be reflected in other source file because physical location of both files are different.

non-static global extern int global is a declaration which goes into each source file via header file. All of them are lodged at physically same location. So if you change its value in one of the file, it would be reflected in other files also.


Further readings: storage duration

Internal linkage. The name can be referred to from all scopes in the current translation unit.
Any of the following names declared at namespace scope have internal linkage variables, functions, or function templates declared static

And

external linkage. The name can be referred to from the scopes in the other translation units. Variables and functions with external linkage also have language linkage, which makes it possible to link translation units written in different programming languages.
variables and functions not listed above (that is, functions not declared static, namespace-scope non-const variables not declared static, and any variables declared extern)

Upvotes: 0

jensa
jensa

Reputation: 2890

The static keyword makes the variable only have "file scope", i.e. limited to that translation unit. The extern keyword is used for variables where you need to tell the compiler that this variable is defined somewhere else. The extern keyword therefore prevents a definition, it simply makes it a declaration.

int a; // declaring and defininig an integer
extern int b; // declaration, tell the compiler that the definition of b is somewhere else

For function this is implicit:

void foo();

is the same as

extern void foo();

Upvotes: 2

Related Questions