Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About Tentative definition

Tags:

c

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; } 
like image 996
Sadique Avatar asked Jun 22 '10 18:06

Sadique


People also ask

What is an example of tentative?

The definition of tentative is not definite or final. An example of tentative is possible, though not definite, plans to go to the movies sometime on Friday. A trial; an experiment. Of or pertaining to a trial or trials; essaying; experimental.

Was the meaning of tentative?

adjective. If someone is tentative, they are cautious and not very confident because they are uncertain or afraid. My first attempts at complaining were rather tentative. She did not return his tentative smile. Synonyms: hesitant, cautious, uncertain, doubtful More Synonyms of tentative.

What is tentative in a sentence?

1. I passed on my tentative conclusions to the police. 2. We made a tentative arrangement to meet again next Friday.

Is Tentative a feeling?

If someone gives you a tentative smile or nod, the person feels hesitant or unsure about something. In this case, its opposite is confident.


2 Answers

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?

like image 53
Tyler McHenry Avatar answered Oct 08 '22 00:10

Tyler McHenry


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.

like image 34
Jerry Coffin Avatar answered Oct 08 '22 02:10

Jerry Coffin