About Tentative definition

Posted on

About Tentative definition

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

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?

Also, what is wrong with this code:

int main(void){
    return 0;

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?

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.

Leave a Reply

Your email address will not be published. Required fields are marked *