1
/*How does this code compiles and executes in C?*/
#include <stdio.h>
int x= 9; //Varaible initialization
int x; //Variable declaration, same name

int main(void) {
    printf("%d", x); //output: 9
    return 0;
}

There should be an error while compilation as same variable is used. How come the code compiles?

4 Answers4

1

From standard 6.9.2p2 you can know about this - this is known as tentative definition.

From here

A tentative definition is an external declaration without an initializer, and either without a storage-class specifier or with the specifier static.

A tentative definition is a declaration that may or may not act as a definition. If an actual external definition is found earlier or later in the same translation unit, then the tentative definition just acts as a declaration.

int i1 = 1;     // definition, external linkage
int i1;         // tentative definition, acts as declaration because i1 is defined.

So from these two references it is clear that the second one merely boils down a declaration or valid tentattive definition on standard words and the value it takes is the one assigned that is 1.

Sane goes here for you with the value of x being 9.

Community
  • 1
  • 1
user2736738
  • 30,591
  • 5
  • 42
  • 56
0

You can declare a global variable or function as many times as you wish. But you must have only one definition. The first line assigns the variable a value, so it is a declaration and definition combined. The second line does not assign a value, so it is only a declaration.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

The second one is not a definition. It's a declaration.

From the ISO C standard draft N1570, Section 6.9.2:

1 If the declaration of an identifier for an object has file scope and an initializer, the declaration is an external definition for the identifier.

2 A declaration of an identifier for an object that has file scope without an initializer, and without a storage-class specifier or with the storage-class specifier static, constitutes a tentative definition. If a translation unit contains one or more tentative definitions for an identifier, and the translation unit contains no external definition for that identifier, then the behavior is exactly as if the translation unit contains a file scope declaration of that identifier, with the composite type as of the end of the translation unit, with an initializer equal to 0.

So when you write

int x;

in file scope, it's not necessarily a definition. It's a declaration, which is fine to be repeated.

This is called tentative definition in C. See more here, here and here.

Community
  • 1
  • 1
iBug
  • 35,554
  • 7
  • 89
  • 134
  • 3
    Yet if you deleted the first definition, the second one would become a definition. C is strangely non-local like that. – Kerrek SB Feb 18 '18 at 15:49
0

In Global scope two variable with same name is possible but one should be un-intialized(weak symbol) and another one should be initialized(strong symbol).

int x= 9; /* strong symbol i.e declaration with definition*/
int x; /* weak symbol i.e only declaration */

while choosing among two, priority is given two strong symbol.

Note : compiler won't allow to have two strong symbol at a time, that would be a re-definition of variable.

Achal
  • 11,821
  • 2
  • 15
  • 37