Difference between Declaration and Definition.

12 views
Skip to first unread message

TheSuyog

unread,
Mar 2, 2011, 3:32:00 AM3/2/11
to Knowledge Test
Can somebody differentiate between declaration and definition of a
"variable".

Ravina soni

unread,
Mar 3, 2011, 1:19:48 PM3/3/11
to knowledge-...@googlegroups.com


int i ;  // declaration
i = 29 ;  // definition  actually its assignment but i think can be said definition of variable

i think so....

Pallavi Jajoo

unread,
Mar 6, 2011, 12:01:20 AM3/6/11
to knowledge-...@googlegroups.com


In additn to wat ravina said..

declartin specifies dat der is an object created of any type bt it not creates a m/r storage 4 dat...and definition also causes m/r storage......

TheSuyog

unread,
Mar 6, 2011, 8:25:29 AM3/6/11
to Knowledge Test
@Ravina: Ok, how you defined is acceptable as this is what happens in
case of functions, but for variables the fact is that they are
assigned the memory when they are declared. @Pallavi: Object creation
= assigning memory to it, how can an object be created be if it is not
assigned any memory...?
There is a bigger difference between declaration and definition of
variables than just that. Try finding out. :)

rajas

unread,
Mar 6, 2011, 8:38:11 AM3/6/11
to knowledge-...@googlegroups.com


A declaration introduces an identifier and describes its type, be it a type, object, or function. A declaration is what the compiler needs to accept references to that identifier.



A definition actually instantiates/implements this identifier. It's what the linker needs in order to link references to those entities.


A definition can be used in the place of a declaration.

TheSuyog

unread,
Mar 6, 2011, 8:41:35 AM3/6/11
to Knowledge Test
@Rajas: for example?

TheSuyog

unread,
Mar 6, 2011, 8:51:51 AM3/6/11
to Knowledge Test
Let me hint you. The difference has got something (actually
everything) to do with "extern". You are gonna find very confusing
answers on this topic...

Ravina soni

unread,
Mar 6, 2011, 9:15:41 AM3/6/11
to knowledge-...@googlegroups.com


@Himanshu sir ab aap he bata do wht exact ans .........kya hena jada dimag nahi lagate jb pata ho ans milne wala h ......... :D 

Dhruvesh harsola

unread,
Mar 6, 2011, 9:28:36 AM3/6/11
to knowledge-...@googlegroups.com, Ravina soni


waah kya baat kahi hai ravina... "Himanshu Sir" sishyo ko itna mat tadpaao..exact ans. bata do..:P




SONALI-

unread,
Mar 6, 2011, 10:36:43 AM3/6/11
to knowledge-...@googlegroups.com
In the Defintion of a variable space is reserved for the and some initial value is given to it, 
whereas a declaration only identifies the type of the variable for the function . 
thus definition is the place where the variable is created or assigned storage
whereas declaration refers to places where the nature of the variable is stated but no storage is allocated.

DECLARATION : declaration only creates the variable name and nothing exists against it. i.e no memory space is consumed.
eg. extern a;
DEFINITION : definition declares as well as assigns some value to the variable.
eg. int a; 
even though we are not mentioning any value default value is provided by the C compiler.
eg. int a=10;

SONALI-

unread,
Mar 6, 2011, 10:39:53 AM3/6/11
to knowledge-...@googlegroups.com
m i ryt himanshu sir..::PP

On 6 March 2011 21:06, SONALI- <shonal...@gmail.com> wrote:






rajas

unread,
Mar 6, 2011, 12:13:52 PM3/6/11
to knowledge-...@googlegroups.com


  1. extern int var;   //declaration. var is an int in another file with external linkage
  2. extern int var = 20;  //definition. var is created here with a value of 20
  3.                              //and is to accessible from other files.
  4. int var = 20;     //definition. Same as extern int var = 20;
  5. static int var = 20;  //definition. var is an int with a value of 20 that has internal linkage
  6.                             //var CANNOT by accessed from another file.
  7.  

rajas

unread,
Mar 6, 2011, 12:17:52 PM3/6/11
to knowledge-...@googlegroups.com
         The "extern" declaration in C is to indicate the existence of, and the type of, a global variable or function.

rajas

unread,
Mar 6, 2011, 12:29:19 PM3/6/11
to knowledge-...@googlegroups.com

CHECK THIS ONE MORE ANSWER :-


In a header file, you can have a line like this:

extern int x; //declaration

Because of the extern modifier, this tells the compiler that there is an int named x somewhere. The compiler doesn't allocate space for it - it just adds int x to the list of variables you can use. It'll only allocate space for x when it sees a line like this:

int x; //definition

You can see that because only the int x; line changes your executable, you can have as many extern int x; lines as you feel like. As long as there's only int x; line, everything will work like you want it to - having multiple declarations doesn't change a thing.

A better example comes from C++ (sorry if this is a C-only question - this applies to structs as well, but I don't know the syntax off the top of my head):

class Pineapple; //declaration

Pineapple* ptr;  //this works
Pineapple pine;  //this DOES NOT work

This declaration tells the compiler that there's a class called "Pineapple". It doesn't tell us anything about the class (how big it is, what its members are). We can use pointers to Pineapples now, but we can't yet have instances - we don't know what makes up a Pineapple, so we don't know how much space an instance takes up.

class Pineapple
{
public:
   
int ounces;
   
char* name;
}; //definition

Pineapple* ptr;   //still works
Pineapple pine;   //this works now too!
//we can even get at member variables, 'cause we know what they are now:
pine
.ounces = 17;

After a definition, we know everything about the class, so we can have instances, too. And like the C example, you can have multiple declarations, but only one definition.

Reply all
Reply to author
Forward
0 new messages