Home > Enterprise >  How to properly define external variables?
How to properly define external variables?

Time:04-14

In the program

case0.c

int main()
{
    int x;
    x = 4;
}

x is first declared as an int, and memory is allocated for x on the following line. No problems here.

However, if I write the files (as part of a bigger program)

case1.c

extern int x; 
x = 4; 

I get, from gcc, warning: data definition has no type or storage class. Similarly, if I do

case2.c

extern int x = 4;

gcc also doesn't like it and gives warning: 'x' initialized and declared 'extern'

The only case that doesn't throw any warnings is

case3.c

extern int x;
int x = 4;

Why does case 1 give an error, when case 0 doesn't? Is case 3 the one and only way I should define external variables?

CodePudding user response:

x is first declared as an int, and memory is allocated for x on the following line. No problems here.

No, that is not what happens. Inside a function, int x; defines x, which reserves memory for it. Then x = 4; stores a value in that memory.

extern int x;
x = 4;

extern int x; declares there to be an x but does not define it. If the program uses this x, it should define it somewhere else.

Outside a function, only declarations should appear. However, x = 4; is a statement, so it is not proper outside a function.

extern int x = 4;

This is valid C, but it is unconventional usage, so the compiler warns you. Conventionally, we write int x = 4; to define and initialize x, and we write extern int x; to declare x without defining it.

extern int x = 4; is defined by the standard; in this context, it is effectively the same as int x = 4;. But programmers generally do not use that form.

(If there is a visible prior declaration of x, such as static int x;, then extern int x = 4; does differ from int x = 4;. extern int x = 4; will refer to the x of the prior declaration, whereas int x = 4; will attempt to create a new x.)

extern int x;
int x = 4;

extern int x; declares x but does not define it.

int x = 4; defines x and initializes it.

Is case 3 the one and only way I should define external variables?

If you only need to use x in one translation unit, you can use int x = 4; by itself, without extern int x;. If you need to use x in multiple translation units, you should put extern int x; in a header file and include that header file in each source file that uses x, including the one that defines it.

CodePudding user response:

"Is case 3 the one and only way I should define external variables?"

No, not if it is to be visible and usable by other source files in the project. If it is only to be used in the file in which it is created, there is no reason to give it extern duration.

The reason to use variables with extern duration is when sharing a single variable between multiple source files is necessary.

A common way (place) to create extern variables to support that scenario :

  • Declare variable with extern modifier in a header file.
  • Define the extern variable in a project file with visibility to its declaration. (eg, can be either a .h or .c . More commonly done in .c) If defined in a file other than the declaration file, file must #include the declaration file.
  • Use the extern variable in any source file by # including its declaration file

Example:

file.h

//anywhere in header file
extern int g_number;//declare extern variable.  
                    //Note extern keywork required only during declaration
                    //Not during definition

//prototypes
void output_extern_variable( void );
void update_extern_variable( void );    
...

file.c

//in file global space
#include "file.h" 

int g_number = 25;//define extern variable
int main(void)
{
    output_extern_variable();
    update_extern_variable(); 
    output_extern_variable();

...

file2.c

#include "file.h"
...

void output_extern_variable(void)
{
    printf("Value of g_number is: %d\n", g_number);
}

void update_extern_variable( int *var )
(
    g_number  = 1;
)
  •  Tags:  
  • c
  • Related