What is the difference between Definition and Declaration of a variable? Am not talking about function, but variable.
When I googled, I found these two answers which are quite contradictory! While one says "definition occures once through the program( memory is allocated once ), but the declaration can occur many times.", the other says "This means that the declaration is only one throughout the program but definitions can be many."
Answer 1:
definition defines the memory area ( allocates the memory ) for the variable and the declaration tells about the signature of the variable ( type and size to be considered). definition occures once through the program( memory is allocated once ), but the declaration can occur many times.
OR For a variable, the definition is the statement that actually allocates memory. For example, the statement:

is a definition. On the other hand, an extern reference to the same variable:
extern long int var;
is a declaration, since this statement doesn't cause any memory to be allocated. Here's another example of a declaration:
typedef MyType short;

Answer 2
Declaration can come only once and definition can come many times in the program.
Let's take an example,

Line 1: #include<stdio.h>
Line 2:
Line 3: void main()
Line 4: {
Line 5: int x; //declaration
Line 6:
Line 7: x=10; //definition
Line 8: }

In the above program, on the line 5 what appears is declaration and on line 7 the definition is there.
Now if we declare the variable x again in the program we will get the error saying that the variable is already declared.
Whereas if we assign some new value to variable x, say 20
i.e.
x=20;
this is totally fine and we can assign any number of values in the same program. This means that the declaration is only one throughout the program but definitions can be many.

Can anyone give me the proper answer?

Thank-you in advance.

Look at it this way.

A declaration is like your street address. It isn't where you live, just a reference to the location. It simply says that your place (or the variable) exists somewhere.

A definition is the house/apartment itself. It holds all your stuff -- like a variable.

You can have your address (declaration) on letters, your license, in your bank account, many places. But your house (definition) can only be in one physical place.

int x; is the definition.
x = 10 is not a definition. It's an assignment. Like buying a new chair. It just goes into the space that's defined.

While one says "definition occures once through the program( memory is allocated once ), but the declaration can occur many times.", the other says "This means that the declaration is only one throughout the program but definitions can be many."

The first one is sufficiently accurate and the second one is completely wrong. I suspect the second one was written by someone who doesn't really understand the terminology and is using the wrong terms for the concept they're trying to explain.

Walt's explanation is reasonable, but a somewhat simplified version of how C handles object declarations and definitions. I'd recommend going with two guidelines that remove the special cases and leave you with a very simple result:

  1. All declarations are qualified with the extern keyword and have no initializer.
  2. All definitions include an initializer.

With these guidelines, int x; is no longer an option. It can either be a declaration:

extern int x;

Or a definition:

int x = 10;

Now there's no ambiguity and the two are easy to identify.

x = 10;, of course, is an assignment and completely unrelated to declarations and definitions excepting the assumption that by naming the object there's a declaration in scope and by using the object it's already been defined. ;)

commented: quite good! +0

As deceptikon said, a declaration of variable is saying to compiler "I will use a variable it is defined before and on another file."

commented: why are you repeating it ? +0

@deceptikon when i use extern int i; and it is also present in other file, then after that i can use it normally ? right ? like x=10 or int x=10 ? i think after declaring i just need to use x=10 only as i have already specified that it is int and extern. am i right ?

secondly, we say that declaration don't allocate memory, that means when i am writing extern int x then it will refer that variable present in the other file automatically ?

thirdly ,
will it be wrong to write

extern int x;

int x=10;

in the same file ?

when i use extern int i; and it is also present in other file, then after that i can use it normally ? right ? like x=10 or int x=10 ? i think after declaring i just need to use x=10 only as i have already specified that it is int and extern. am i right ?

When you say extern int x;, you're saying that x is not defined here. Therefore it doesn't act as a definition at all. Whether a subsequent x = 10; succeeds or not depends on the presence or absence of a definition somewhere else (which may be in the same translation unit).

secondly, we say that declaration don't allocate memory, that means when i am writing extern int x then it will refer that variable present in the other file automatically ?

That's a reasonable statement.

thirdly, will it be wrong to write

No, but it would probably make little sense to do so since a definition is also a declaration.

so @deceptikon when i am writing extern int i; and then using x in my program . So according to you, it it will not work because it is not defined ? But my question is that why will it not work as x is defined else where so it will present in trslation unit after linking from different file.

secondly, isn't this an error as i am saying to compiler that x is defined else where and again i am defining another x, so isn't it a re defintion of x error ? i don't know why am i confused so much in this thing as i know much about these already. thanks

So according to you, it it will not work because it is not defined ?

It won't work if it's not defined. It if is defined then it'll work fine because that's the use case for extern.

secondly, isn't this an error as i am saying to compiler that x is defined else where and again i am defining another x, so isn't it a re defintion of x error ?

I fail to see what's so complicated about this.

  • You may have one definition, period.
  • You may have many declarations.
  • Code will not compile without declarations.
  • Code will not link without definitions.

That's all, don't overthink it.

ya! i know there is no difficulty in this concept! i am thinking as if i am fool :p but still i wana ask again.

when i have used extern int i; now it will refer i which is outside or extern to the file . right ? then in the next line, i am writing int i=10; then again there is another i for the compiler. So now which i will be used ? thanks.

when i have used extern int i; now it will refer i which is outside or extern to the file . right ? then in the next line, i am writing int i=10; then again there is another i for the compiler. So now which i will be used ? thanks.

If you have two definitions of i in different files then the code will fail to link. If the definition in the same file as the extern declaration is the only definition then the extern declaration will refer to it.

If the definition in the same file as the extern declaration is the only definition then the extern declaration will refer to it. I didnt get this line. will you please re-write it in different form ?

Thanks walt, thank you decepticon...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.