In C, is it possible to declare a variable multiple times?

I have code below C, and I expect it to throw an error, such as "multiple variable declaration", but it is not.

#include <stdio.h> int i; int i; int main() { printf("%d",i); return 0; } 

Now the conclusion is 0 , but why?

And one more thing below the code gives an error, which is expected

 #include <stdio.h> int main() { int i; int i; printf("%d",i); return 0; } 

O / p - error stating re declaration i

+6
source share
1 answer

The first definition of i is a preliminary definition (the second is also a preliminary definition). They are de facto definitions (and the definitions also serve as declarations), without error.

Quote from Standard :

6.9.2 / 2

The declaration of an identifier for an object having a scope without an initializer, and without a storage class specifier or with a static storage class specifier is a preliminary definition. If the translation unit contains one or more preliminary definitions for the identifier, and the translation unit does not contain an external definition for this identifier, then the behavior is exactly the same as if the translation unit contained the declaration of the visibility of the file identifier with a composite type at the end of the translation block with the initializer equal to 0.

+17
source

Source: https://habr.com/ru/post/893259/


All Articles