Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C implicit extern for global variable, when does it happen, how does it work

I'm trying to understand the ways in which a C global variable can be shared between multiple files (compilation units). I've read the excellent question and answer here. However after doing a few tests I'm still left with some stuff I don't get:

Basically my question would be: if there's a variable declared (but not defined) in a header WITHOUT the extern keyword, is it ok to simply include that header in various compilation units in order to make available that variable to all those compilation units? In this scenario it's implied that one (and only one) compilation unit contains the code for initializing (defining?) that variable, and it will be called first before the other compilation units try to do anything with that variable. If all this is true, is this procedure what's called "implicit extern" ?

I'll illustrate my question with an example:

Header "MyCommonHeader.h" contains:

//MyCommonHeader.h
int* i; //pointer to an int

File MyFirstHeader.h contains:

//MyFirstHeader.h
void changeIt(int newValue);

File MyFirstSource.c contains:

//MyFirstSource.c
#include "MyFirstHeader.h"

 void changeIt(int newValue) {
    *i = newValue;
 }

File MySecondSource.c contains:

//MySecondSource.c
#include "MyCommonHeader.h"
#include "MyFirstHeader.h"

void main() {
   i = malloc(sizeof(int));
   changeIt(10);
   *i = 23;
}

Does the above code operates with the same i variable everywhere? Do I need to add externanywhere?

like image 542
Shivan Dragon Avatar asked Sep 15 '25 01:09

Shivan Dragon


1 Answers

/* file.h */
int* i;

is a tentative definition of the i variable. This means that if there is no other (external) definition for that variable in the translation unit, it will be defined just once (initialized to 0). If there is exactly one matching (external) definition of i elsewhere in the translation unit, that definition will be used, and the tentative definition above will behave as a declaration.

As a common extension, compilers extend this behavior across translation units. This means, for such compilers, you can safely include that header file in as many translation units as you want, and there would still be only one definition of i.

It would have been different if you had also explicitly initialized i in the header file :

/* file.h */
int* i = 0;

This is an actual definition (not tentative), and you can only include that header file in one compilation unit, or you'd get a multiple definition error.

The better way, is to define the variable in a .c file, and use extern in the header file :

/* file.h */
extern int* i;

/* file.c */
int* i = 0;

This makes it absolutely clear that there is only one definition (the one in the .c file), and that every compilation unit where the header file is included will refer to that definition.

like image 186
Sander De Dycker Avatar answered Sep 17 '25 15:09

Sander De Dycker