3

This is the most confusing part for me from section Global Variables and linkage properties.

  extern int g_var1;

Statement could be something like this when defining an external non-const global variable. I think I will write exactly the same for using that variable (through forward-declaration) in some other file. If both the statements are same, how C++ knows whether the variable was declared or was defined in a file?

3 Answers3

2

If I understand your question correctly, you shouldn't write exactly the same in another file (namely, you shouldn't write "extern int g_var1" in two files). A good practice is to declare some variable global in a header file; make the definition in a cpp file that includes this header file. After doing this, you can use this variable in all of the files that will include the header file.

To illustrate, an example would be something like this:

variables.hpp

#pragma once
extern int g_var1;

variables.cpp

#include "variables.h"

int g_var1 = 1;

main.cpp

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

using namespace std;

int main(){
    cout << g_var1 << endl;
}
ozdm
  • 153
  • 3
  • 11
1

A statement of the form

extern int g_var1; // this is a declaration

is a declaration of a variable. The keyword extern makes sure of this. If you write

int g_var1; // declare and define variable

you define it as well. You can declare a variable as many times as you like, but define it only once. You could therefore write

extern int g_var1;

in those files where you need to use the variable. Then during linking the compiler will resolve the definition of the variable (provided that you give the definition in some file of course).

jensa
  • 2,792
  • 2
  • 21
  • 36
  • Yes you are right. Defining a global variable by prefixing keyword extern is redundant. But my question is for a case where the extern keyword is used to define and not declare a global variable. Defining that way is legal because compiler doesn't throw any error. –  Jul 21 '15 at 09:31
  • What do you mean "for a case where the extern keyword is used to define and not declare a variable"? The extern keyword makes the statement be a declaration, not a definition. – jensa Jul 21 '15 at 09:33
  • When a beginner writes a program and wishes to define a variable...he can mistakenly use "extern" for defining that variable but compiler won't complain. –  Jul 21 '15 at 09:38
  • Ah, then I understand what you meant. Sure, I agree. However, if the variable isn't defined anywhere the compiler will complain. – jensa Jul 21 '15 at 09:43
0

When you say extern int g_var1; the variable is just declared, and later when you have to use it you can use it directly.

file1.cpp:

int g_var1;

File2.cpp:

extern int g_var1;

You need not to write extern everytime. Though i would suggest if you have to use global variables place them in a separate header file.

Nishant
  • 1,635
  • 1
  • 11
  • 24
  • Nishant I totally agree with you. But think of a case where a beginner (like me) uses the "extern" keyword for defining the global variable. Compiler never treats it as error. What then? –  Jul 21 '15 at 09:36
  • Do you mean to say something like this? extern int g_var1= 0; Please refer this, hope it would help you. http://stackoverflow.com/questions/17090354/why-does-initializing-of-an-extern-variable-locally-inside-a-function-give-an-er – Nishant Jul 21 '15 at 10:16
  • Yes...but because it's non-const, initialization is not must. –  Jul 21 '15 at 10:16