0

So far I have understood the following: A variable declaration is the declaration of a type and name of a variable without allocating memory space for it. A variable definition means that the variable is declared and memory space is allocated for it. So it has nothing to do with the initialization of the variable, whether you speak of a definition or a declaration. In C, a declaration is always a definition e.g. if one write int i;. But there is one exception. If you write extern int i; no memory space is allocated, only the variable is declared. So int i; is always declaration and definition at the same time. But extern int i; is just declaration.

Is it true that in C you can declare a variable as often as you want, but you can only define the variable once? I ask because I've tried the following and the compiler results confuse me. I use gcc and don't set the -std flag. Neither this program:

int i;
int i;

void main(void){
i = 2;
}

nor this program:

int i=0;
int i;
void main(void){
i = 2;
}

lead to problems. The compiler compiles both without error. I would have expected since I didn't use the "extern" keyword here that the compiler would say something like "error: multiple definition". But it doesn't give an error message. Is it possible that the compiler automatically writes an "extern" before all global defined "int i;" if I don't initialize them at the same time? Isn't it then superfluous for the programmer to ever use the extern keyword for variables since the compiler will do that automatically anyway?

I think my considerations are confirmed by the following behavior. The following programs return errors:

int i;
i=0;
void main(void){
i = 2;
}

leads to:

"warning: data definition has no type or storage class
 i=0;
warning: type defaults to 'int' in declaration of 'i' [-Wimplicit-int]"

and

float i;
i=0;
void main(void){
i = 2;
}

leads to:

"warning: data definition has no type or storage class
 i=0;
 warning: type defaults to 'int' in declaration of 'i' [-Wimplicit-int]
 error: conflicting types for 'i'
 note: previous declaration of 'i' was here
 float i;"

So to me again it looks like there is an implicit "extern" before the first int i; respectively float i; is written because they are not assigned a value. As a result, no storage space is allocated for i. But there is no other file in which storage space is allocated for i. Therefore there is no definition for i and the compiler therefore thinks in the 2nd line that i should be defined here. Therefore there are no problems with the 1st program because the automatic type assignment fits, but with the 2nd program it no longer fits, which is why an error is returned.

The following program also throws an error:

void main(void){
int i;
int i;
}

If I write the declaration (and thus also the definition) in a scope, the compiler returns the following error message.

"error: redeclaration of 'i' with no linkage int i;
 note: previous declaration of 'i' was here int i;"

I can only explain it again with the fact that the compiler does not automatically set an "extern" before a variable that is not a global variable and therefore there are 2 definitions here. But then I ask myself why is it called redeclaration and not redefinition or multiple definition?

It would be very nice if someone could confirm my assumptions or enlighten me on how to understand it correctly. Many Thanks!

sidyman
  • 43
  • 3

1 Answers1

0

A variable declaration is the declaration of a type and name of a variable without allocating memory space for it.

Even if memory is reserved for an object, it a declaration. We do not exclude definitions from declarations; there are declarations that are definitions and declarations that are not definitions.

The declaration x = 3; causes memory to be reserved for x, but it also makes the name and type of x known, so it declares x.

So int i; is always declaration and definition at the same time.

Not quite. Inside a function, int i; is a definition. Outside of a function, int i; is a tentative definition. This is a special category that was necessary due to the history of C development. The language was not designed all at once with foresight about how it would be used. Different implementors tried different things. When a standard for the C language was developed, the committee working on it had to accommodate diverse existing uses.

When there is a tentative definition, the program can still supply a regular definition later in the translation unit. (The translation unit is the source file being compiled along with all the files included in it.) If the program does not supply a regular definition by the end of the translation unit, then the tentative definition becomes a regular definition as if it had an initializer of zero, as in int i = 0;.

Some C implementations treat multiple tentative definitions of an identifier in different translation units as referring to the same object. Some C implementations treat them as errors. Both behaviors are allowed by the C standard.

Is it true that in C you can declare a variable as often as you want, but you can only define the variable once?

Not always. Variables with no linkage (declared inside a function without static or extern) may not be declared more than once. (An identical declaration can appear inside a nested block, but this declares a new variable with the same name.)

Repeated declarations must have compatible types, and there are additional rules about which repeated declarations are allowed. For example, an identifier may not be declared with static after it has been declared with extern.

The compiler compiles both without error.

As described above, int i; outside a function is a tentative definition. Initially, it acts only as a non-definition declaration. So it may be repeated, and it may be replaced by a regular definition.

So to me again it looks like there is an implicit "extern" before the first int i;

No, there is not. int i; is a tentative definition, and it has nothing to do with the error messages you are getting. The error messages “data definition has no type or storage class” and “type defaults to 'int' in declaration of 'i'” are from the i=0;. This is a statement, not a declaration, but the C grammar does not provide for statements outside of functions. Outside of functions, the compiler is looking for only declarations. So it expects to see a type, as in int i=0;. The first message tells you the compiler does not see a type or a storage class. The second message tells you that, since it did not see a type, it is assuming int. This is a relic of old behavior in C where the int type would be taken as a default, so it could be left off. (Do not use that in new C code.)

The following program also throws an error:

Inside a function, int i; is a definition, so two of them causes multiple definitions of i.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312