A global variable, and a variable declared outside of all functions with extern - is that the same or different?
A global variable, and a variable declared outside of all functions with extern - is that the same or different?
test1.c:
test2.h:Code:#include <stdio.h> #include "test2.h" int global_var = 0; int main() { global_var = 1; printf("%d\n", global_var); test(3); printf("%d\n", global_var + 1); return 0; }
test2.c:Code:extern int global_var; void test(int);outputsCode:#include <stdio.h> #include "test2.h" void test(int x) { printf("%d\n", global_var + 1); global_var = x; printf("%d\n", global_var); }
1
2
3
4
Is that the right set up of a global variable, the right use of extern? I tried deleting extern from the .h file and it made no apparent difference.
Thanks.
Last edited by BpB; 04-10-2017 at 12:42 AM.
A global variable has external linkage by default and when you use extern ( for globals specifically ) you're practically saying "This is just a declaration, not a definition. Expect the definition elsewhere."
This is useful if you want your variable to be accessible through a header file and not be defined multiple times as a result of multiple translation units.
EDIT:
The reason you're getting no errors for removing extern in your example is because multiple declarations of globals in the same file are "ignored".
Last edited by GReaper; 04-10-2017 at 12:47 AM.
Devoted my life to programming...
Not having extern in that test2.h file made no difference though, or does it?
Doesn't make any difference I moved the global_var define from test1.c to test2.c, in the same kind of position outside functions. With and without extern in the .h file, no difference.
A variable (or more precisely, an identifier for an object) not previously declared that is declared at file scope without being declared static has external linkage, i.e., if a similiar declaration occurs in another translation unit (i.e., source file + headers), it refers to the same object.
The issue is that some declarations of variables are also definitions, and some are tentative definitions. For example, you could do this, because of the declarations of n are tentative definitions:
But you cannot do this, because the declarations of n are definitions, and an object can only be defined once:Code:#include <stdio.h> int n; int n; int main(void) { n = 123; printf("%d\n", n); }
Using extern (without an initialiser, and before a declaration of the identifier that declares it static and therefore having internal linkage) makes it explicit that a declaration of an identifier for an object declares the identifier to have external linkage, and that that declaration is not a definition of the object at all. Hence, it makes sense to declare global variables extern in headers because you do not want to run into the problem of including a header in multiple translation units, only to find that the declaration of the global variable is actually a definition, hence you are breaking the rule that an object can only be defined once, thus compilation (or more precisely linking) will fail. Of course, this still means you need to define the object somewhere, but you can do so in exactly one source file.Code:#include <stdio.h> int n = 123; int n = 123; int main(void) { printf("%d\n", n); }
Last edited by laserlight; 04-10-2017 at 01:42 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I semi-understood that. I get the gist. Using extern, where appropriate rather than not, even though it's not outright required, is a good idea as it makes things explicit and avoids some possible problems. Ok that's great, thanks.