Skip to content
Advertisement

How to declare a global variable in an header file and how he need to be in the c file

i have a serious problem to understand how to declare a global variable in an header file and how he need to be in the c file.

In my .h : extern struct my_global_variable glob;

and on my .c i add to reference it : struct my_global_variable glob;

Is it like that ?? Thanks you for your answer and have a good day/night depend πŸ˜›

Advertisement

Answer

Declare and define the global variable only in 1 .c file and use extern to declare the global variable only in the other .c files.

Example with 3 source files: g.h, g1.c and g2.c:

JavaScript

You compile with:

JavaScript

And execution says:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement