Reputation: 3935
the question is can we call variable A "global variable"? From the one hand A is static global variable, so it is global by definition, from the other global variable must be available in every point of your program, not only in the current translation unit. Thanks.
#include<stdio.h>
static int A;
void main()
{
...
}
Upvotes: 2
Views: 218
Reputation: 28837
Think of it as a module variable, as it is visible to one module (intended here as "translation unit"), rather than visible to all modules.
Upvotes: 1
Reputation: 76245
As others have said, it depends on why you care about it being "global". In a large source file, a static variable can introduce hard-to-trace coupling between functions, much like a non-static function. The difference, of course, is that the static variable is confined to a single source file, but that's small comfort if you've got hundreds of functions in that source file that you have to wade through to figure out where that unexpected modification is coming from.
Upvotes: 1
Reputation: 126777
I don't think there's a specific, widespread terminology to indicate those "static globals"; the problem is that the standard never talks about "global variables" in general, but it distinguishes two orthogonal concepts:
static
local variables; the standard calls all them "variables with static storage duration";static
local variables have local scope.Is it correct to call the "static globals" "globals"? As already said, the standard never defines "global variables", so it should be a matter of taste: they are defined at global namespace scope, but they are not accessible in other modules because of the fact that they have internal linkage.
Still, keep in mind that, generally, when talking about globals in C++ we refer to variables defined at global namespace scope with external linkage. So, all in all, I wouldn't call them globals, the only nonambiguous way to call them I see is "variables defined at global namespace scope with internal linkage".
Upvotes: 2
Reputation: 476950
Your variable A
has static storage and is defined at file scope, and it has internal linkage. The term "global variable" is only a colloquialism that doesn't capture all those nuances completely accurately. The variable is certainly global in the sense that it can be accessed from every scope, and its lifetime is from program start to program end, but because of its internal linkage, it cannot be accessed from outside the translation unit in which it is declared.
Upvotes: 3
Reputation: 258558
No, a static
is not a global because it has internal linkeage. A copy will exist for each TU that defines it.
From the one hand A is static global variable, so it is global by definition
Why is it a static global variable? It's static
, yes, but that's about it.
Global variables in C++ are those declared extern
and defined only once, or contained as static
members (which has a whole different meaning).
Upvotes: 7