-1

As we know extern declaration of variables can be initialized.

#include<stdio.h>

extern int a=5; //will work as both definition and declaration.
int main()
{

}

Why this programme is compiling and running with no error.

extern int a=5; //will work as both definition and declaration.
int main()
{

}

int a; // isn't it second definition??
user2333014
  • 157
  • 1
  • 1
  • 4

1 Answers1

4

C has a concept of a "tentative definition". Most definitions without initializers are "tentative", so an arbitrary number of them are allowed, as long as they don't conflict with each other. At the end of the translation unit, the definition of the variable becomes basically the composite of those (e.g., if one definition says the variable is const and another says it's volatile, the variable will end up as both const and volatile.

A definition with an initializer is never tentative, so only one of them is allowed.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Which declaration specifiers can differ between tentative definitions? cv-qualifiers, storage specifiers (extern, static?), what else? – Kos Apr 29 '13 at 17:41