41

I read from a book about tentative defination that,

A tentative definition is any external data declaration that has no storage class specifier and no initializer. A tentative definition becomes a full definition if the end of the translation unit is reached and no definition has appeared with an initializer for the identifier

Please explain what the above statement means. Also, the difference between Declaration and Definition? I got mixed up due to this. :( And why doesn't this program give an error:

#include <stdio.h>

int a;      //Tentative definition
int a;      //similarly this declaration too.
int main()  //not getting any error with this code why its so?
{
    printf("hi");
} 

Also, what is wrong with this code:

#include<stdio.h>
printf("Hi");
int main(void){
    return 0;
}
Sadique
  • 22,572
  • 7
  • 65
  • 91

2 Answers2

71

A variable declaration says, "there is a variable with the following name and type in the program".

A variable definition says, "Dear Mr. Compiler, please allocate memory for a variable with the following name and type now."

So there can be multiple declarations for the same variable, but there should be only one definition.

In C, pure declarations (that are not also definitions) are preceded with the keyword extern. So, since you do not have this keyword in your first example, what you have is two definitions. On its face, this would seem to be a problem (and is in fact an error in C++), but C has a special "tentative definition" rule which allows multiple definitions for the same variable in the same translation unit so long as they all match and at most one has an initializer. The C compiler, behind the scenes, combines all of the tentative definitions into a single definition.

Had you attempted to initialize both definitions, like this:

int a = 1;
int a = 2;

Then you would have had an error.

Your second question is more straightforward. In C, you simply cannot have executable statements outside of the body of a function. It's just not allowed. Think about it: when would you expect it to run if it were allowed?

M.M
  • 138,810
  • 21
  • 208
  • 365
Tyler McHenry
  • 74,820
  • 18
  • 121
  • 166
  • @TylerMcHenry: It's illegal in both C and C++ but C compilers implement this as a common extension. See [this answer](http://stackoverflow.com/a/6371156/183120). – legends2k Mar 03 '14 at 12:23
  • 9
    @legends2k No, this is not illegal in C...in the question that you link to (and also the question that the answer you link to links to) involves multiple files. Multiple (compatible) tentative definitions in the same file is not a violation. The violation in that question comes because, at the end of the translation unit, if there are tentative definitions with no full definition, a full definition is implied. That plus multiple files leads to multiple definitions at linking time (except for the common extension of the tentative definition rules to span multiple files). – Theodore Murdock May 18 '15 at 21:59
  • Why it is only legal with global variables, not with local variables? With local variables, it gives redeclaration error? – Rishab Shinghal Dec 24 '17 at 14:13
  • 1
    @RishabShinghal: The only reason C allows it is to be compatible with code written before the "extern" keyword was added. And extern does not apply to local variables where duplicates were never allowed. – Zan Lynx Jan 15 '18 at 19:02
  • "C has a special "tentative definition" rule which allows multiple definitions for the same variable in the same translation unit so long as they all match and at most one has an initializer." In C, if you declare a variable at file scope and specify neither a linkage nor an initializer, then that is usually a declaration and not a definition. Only if no definition exists in the translation unit will one implicit definition kinda sorta be created. Even that's not really true as the linker will look across all translation units for a real definition and use any that it finds (common symbol). – jschultz410 Mar 06 '21 at 22:10
13

The first works because both your definitions of a are tentative, which can be duplicated as often as you see fit. At the end of the translation unit, no non-tentative definition has been seen, so what you've specified for attributes is combined with defaults to give a final definition of a, so it'll have external linkage, static storage duration, and be initialized to 0.

The problem with the second has nothing to do with tentative definitions. Your printf("Hi"); needs to be inside a function to work -- it's not a declaration or a definition (tentative or otherwise); it's just not allowed there.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111