3

I know that variables that can be accessed everywhere in the program are global. Is it a correct definition or we should say "variables declared outside of a block" ? I'm trying to understand how to define them more specifically. I know examples of simple declaring of global variables outside a function (normally after the include's and using's). I know that one can use a forward declaration with the extern keyword. Here is an example of 3 global variables (t, d and c):

#include <iostream>
#include "some.h"
using std::cout;
int t;
extern double d;
int main() {
  extern char c;  // Or here c is not an example of global variable?
  t = 3;
  cout << t;
}

Is that all cases?

Max Sergeev
  • 55
  • 1
  • 5
  • 3
    Would you consider this also? `int& global_int() { static int i = 0; return i; }` – Chad Sep 25 '13 at 21:06

2 Answers2

4

The term "global variable" can be thrown around a lot, and sometimes it's not really that correct because the term "global variable" is not really defined by the standard. It is a very prolific and common term for variables accessible from "anywhere" but this isn't the whole picture (especially since "anywhere" is highly subjective!).

To really grasp this you need to know about two main aspects of variables: storage and linkage There's a great answer here about that specifically.

Let's look at the possible definitions for "global variable":

  • In some circles, "global variable" means any variable with external linkage. This includes every example you gave.

  • In others, internal linkage is considered global as well. In addition to the first group, this would include variables declared outside of a function with static or const specifiers. Sometimes these aren't considered truly global since they can't be accessed outside of that particular compilation unit (which typically refers to the current .cpp file and all its included headers in one blob).

  • Finally, some people consider any static storage variable as global, since its existence is persistent throughout the program's life. So in addition to the first and second groups, a variable declared within a function, but declared static could be called a global. It is feasible to return a reference to these, so they are still accessible by a subjective "anywhere."

Summing it all up with a similar example to yours:

extern int extern_int; // externally linked int, static storage (first group)
int just_an_int;       // by default, externally linked int, static storage (first group)
static int static_int; // internally linked int, static storage (second group)
const int const_int;   // by default, internally linked int, static storage (second group)

int & get_no_link_static_int()
{
    static int no_link_static_int = 0; // no linkage int, static storage (third group)
    return no_link_static_int;
}
Community
  • 1
  • 1
Sam Cristall
  • 4,328
  • 17
  • 29
  • Now I recognize that global variable can be defined like this: namespace { int global_int; } – Max Sergeev Sep 29 '13 at 19:44
  • @MaxSergeev I'd still be careful with that. That is a statically stored, likely externally linked variable in the global namespace. Calling it a global variable isn't really wrong, but it doesn't paint a complete picture. – Sam Cristall Sep 30 '13 at 16:42
1

There is another case:

A static declaration at file scope is global within just the current file:

    static int i;
    int main() {
    etc.
    }
Not Submitted
  • 653
  • 1
  • 7
  • 13