GeekInterview.com
Series: Subject: Topic:
Question: 575 of 589

 What is the difference between declaring a variable and defining a variable?

Declaring a variable means describing its type to the compiler but not allocating any space for it. Defining a variable means declaring it and also allocating space to hold the variable. You can also initialize a variable at the time it is defined.
Asked by: Interview Candidate | Asked on: Mar 6th, 2005
Showing Answers 1 - 6 of 6 Answers

By using declaration we can the name and type of a variable.But it does not allocate space.

By  using definition  we can allocate space for the variable.

  
Login to rate this answer.
Deepak Bhandari

Answered On : Mar 30th, 2007

When we declare a variable we give only the name and data type for that variable but when we define a variable we give the name, datatype for that variable and also give a particular value to that variable. For exemple in C language:

int num (this is declaration of variable)

int num=2 (this is defining of variable)

Yes  1 User has rated as useful.
  
Login to rate this answer.
shiva

Answered On : Apr 9th, 2007

Declaration announces only properties of varaibles and a definition also causes storage to that variable...

  
Login to rate this answer.
Prakash

Answered On : Apr 29th, 2007

As per view int a = 2 is a declaring a variable. But it is called as dynamic intialization.
Can you expalin?

  
Login to rate this answer.
prabhu

Answered On : May 5th, 2007

int num;

This statement is declaration and also defination since it also allocates memory for variable num.

But when you use following statement

extern int num;

It is only declaration since you are not allocating memory here.

Yes  3 Users have rated as useful.
  
Login to rate this answer.

As far as declaration of variable is concerned it only shows the properties of variable and we declare the data type which are to be used by that variable but when it comes to the definition of variable then we alocate a perticular space in the memory

int a(it's the declaration and definition of variables)
extern a(this is the actual definition of the variable)

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.