Pregunta

Why should I use the extern keyword in the following code:

header.h

float kFloat; // some say I should write 'extern float kFloat;', but why?

file.c

#include <stdio.h>
#include "Header.h"

float kFloat = 11.0f;

main.c

#include <stdio.h>
#include "Header.h"

int main(int argc, const char * argv[])
{
    printf("The global var is %.1f\n", kFloat);

    return 0;
}

This code works. The global Variable kFloat defaults to external linkage and static lifetime.

Output is:

The global var is 11.0

I don't understand in which case the problem would occur, can anyone give me an example where it would crash?

¿Fue útil?

Solución

extern float kFloat;

declares kFloat without defining it.

but:

float kFloat;

also declares kFloat but is a tentative definition of kFloat.

Adding extern just suppresses the tentative definition. In a header file you only want declarations, not definitions.

If the tentative definition is included in several source files, you will end up having multiple definitions of the same object which is undefined behavior in C.

Otros consejos

Always put the definition of global variables(like float kFloat;) in the .c file, and put the declarations (like extern float kFloat;) in the header.

Otherwise when multiple .c files include the same header, there will be a multiple definition error.

extern indicates that a variable is defined somewhere in the project (or outside function block) that you want to use. It does not allocate memory for it since you are telling the compiler that this is defined else where.

A variable must be defined once in one of the modules of the program. If there is no definition or more than one, an error is produced, possibly in the linking stage.

Definition refers to the place where the variable is created or assigned storage; declaration refers to places where the nature of the variable is stated but no storage is allocated.

And since it will be accessible elsewhere it needs to be static.

Firstly, it is strange that your code is compiling. It should throw compile time error for double definition of kFloat variable in File.c.

Secondly, if you are trying to use the common variable in two files then it should not be defined in the header.h. You should use extern keyword in header file so that the file which includes the header.h gets to know that it has been defined externally.

Now, you can define the variable globally in any of the c files and then use the variable as a common variable.

Why should I use extern ...?

Well, you shouldn't. Plain and simple. Because you shouldn't use global variables, and they are the only ones that would need the extern keyword.

Whenever you feel tempted to use a global variable, think again. At the absolute maximum, you might need to use a variable with file scope (using the static keyword), typically such a variable would be accompanied by a handful of functions that manipulate/use its value, but the variable itself should not be visible beyond the scope of the file. Usage of global variable only leads to intractably tangled code that is almost impossible to change without introducing a ton of bugs.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top