Connecting Tech Pros Worldwide Help | Site Map

difference between declaration and definition

Newbie
 
Join Date: Oct 2009
Location: kanhipuram
Posts: 2
#1: Oct 4 '09
I have been studying c language in depth and i could not find the difference between declaration and definition.

my lecturers said when a variable is decleared space will be alloted for that variable. But some here say that only during definition the space is alloted.

int a=6;
The above statement is called a declaration and there is no definition following that can somebody say why?

extern int a=6;
The above statement is called a declaration and why is it not called as a definition?

clarify my doubt
thank you.
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,363
#2: Oct 4 '09

re: difference between declaration and definition


The general rule is that a definition allocates memory whereas a declaration just says something exists.

So,

Expand|Select|Wrap|Line Numbers
  1. int a = 6;
is a definiton. An int has been created containing the value 6.

Expand|Select|Wrap|Line Numbers
  1. extern int a;
is a declaration. It says there is an int named a somewhere else.

So these are declarations:

Expand|Select|Wrap|Line Numbers
  1. struct Data
  2. {
  3.     int foo;
  4. };
  5.  
  6. void swap(int* a, int* b);
while these are definitions:

Expand|Select|Wrap|Line Numbers
  1. Data var;
  2.  
  3. void swap(int* a, int*b)
  4. {
  5.     int temp;
  6.     temp = *a;
  7.     *a = *b;
  8.     *b = temp;
  9. }
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,363
#3: Oct 4 '09

re: difference between declaration and definition


Oops. I forgot about this:

Expand|Select|Wrap|Line Numbers
  1. extern in a = 6;

This is a definition. A variable has to be created to contain the 6. In this case the variable is sharable with other implementation files due to the extern.

Note that:

Expand|Select|Wrap|Line Numbers
  1. extern int a;
is a declaration since no variable had to be created.
Newbie
 
Join Date: Oct 2009
Location: kanhipuram
Posts: 2
#4: Oct 4 '09

re: difference between declaration and definition


thank you for your reply
so, when you assign a variable it becomes a definition.
is it correct?
can we say something like that
Familiar Sight
 
Join Date: Jan 2007
Posts: 188
#5: Oct 5 '09

re: difference between declaration and definition


You can have declarations and definitions appropriate to functions also.
consider:
Declarations:
Expand|Select|Wrap|Line Numbers
  1. double sqrt (double);
/*a declaration of the sqrt() function which takes an arguement of type double and returns a double...... note the semi-colon.
or*/
Expand|Select|Wrap|Line Numbers
  1. double x=0;
  2. double sqrt(double x);  
/*declaration of sqrt () function which has a variable 'x' passed to it of type double.(preferred) and returns a double......note the semi-colon.
Definition:*/
Expand|Select|Wrap|Line Numbers
  1. double sqrt(double x)//no semi-colon
  2. {
  3.      return pow(x,0.5);/*one implementation of sqrt() 
  4.                                 within the function body*/
  5.  }
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 828
#6: Oct 5 '09

re: difference between declaration and definition


Quote:

Originally Posted by arun121185 View Post

thank you for your reply
so, when you assign a variable it becomes a definition.
is it correct?
can we say something like that

Not quite. The apparent assignments in whodgson's examples are actually initializers. (An initializer is sufficient, but not necessary, for a definition.) It doesn't matter if a variable is declared or defined; you can assign to it either way.
Expand|Select|Wrap|Line Numbers
  1. extern int i;    // this is a declaration for a variable defined in another source file.
  2. void demo1(void) {
  3.     i = 4;
  4.     }
  5.  
  6. int j;    // this is a definition.
  7. void demo2(void) {
  8.     j = 10;
  9.     }
Reply