If I say
int i=8;
outside of every function, is this definition of a global variable tentative and why?
EDIT: I changed 'conditional' to tentative, I translated it wrong.
If I say
int i=8;
outside of every function, is this definition of a global variable tentative and why?
EDIT: I changed 'conditional' to tentative, I translated it wrong.
If you declare the variable outside of a function scope, then it has static storage duration and by default external linkage. It means that the variable is accessible everywhere in the current translation unit (i.e. the current C file produced by the pre-processor). The variable is also accessible in other translation units, but you need to either declare it there as extern int i;
or tentatively define it as int i;
. Note that if you define the variable as static int i = 8;
at file scope, then the variable will have internal linkage and you won't be able to use it in other translation units, even if declaring there as extern int i;
.
There is no conditional going on here, I don't know what you mean by this word.
EDIT
No, yours is not a tentative definition.
If you define a global variable as you have illustrated, then any function that is defined before that variable will not be aware of the variable unless some declaration has been made before the function, or within the function.
void foo () {
extern int i; /* declare presence of global i */
/* code that uses i */
}
int i=8;
void bar () {
/* code that uses i, does not need declaration */
}
A tentative definition for a global is a bare declaration without an initializer.
int i; /* tentative definition */
void foo () {
/* code that uses i */
}
The tentative definition allows i
to be used like in a forward declaration, however, it will also be treated as a default initialized external definition unless an explicit external definition for i
is found.