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;
}