Difference between variable ‘declaration’ and ‘definition’ ?

Q. What is the difference between variable declaration and variable definition in C programming language ?

Answer: This is one of the most confusing question that every beginner has in mind. Let me explain this  with an example.

Consider the below statement:

int a;

This one is generally called as a variable declaration statement. But, strictly speaking, it is ‘variable declaration-cum-definition’. So what exactly is the difference between them. If we refer to Appendix A8 of “The C Programming Language” written by K & R, it clearly mentions that, “Any declaration that reserve storage is called definition”. It means, if, along with the declaration, if memory allocation is done with it, it is also called as a definition. In the above statement ‘int a’, we are informing the compiler that, the programmer needs to store a value of an integer type for which 4 bytes of memory should be allocated or reserved. Hence this statement is a declaration-cum-definition. Now another question arises in our mind – Is there anything known as “just declaration”. The answer is “Yes”. There is. For example, consider the below statement,

extern int a;

This statement is used to inform the compiler not to allocate/reserve any storage for the variable. It is just an indication to the compiler that, there could a variable named ‘a’ that is defined in any of the compilation units ( or any other module in the current project ) whose reference is used in the current context or file that is being compiled right now. Hence, if the compiler is unable to find its definition in the current file, the compiler should just ignore the usage of that variable in the current context without giving any compile time errors. So, by writing ‘extern int a’, compiler does not reserve storage for ‘a’, as it understands that, memory allocation is done somewhere else and only the referencing is done in the current context. This above statement is called purely as ‘variable declaration’.

In simple words,

Variable Definition: Bring in the scope and reserve storage. Example: ‘int a’

Variable Declaration: Only bring in the scope and memory not reserved. Example: ‘extern int a’

 

Author Information:

Subhash.K.U, Principal Mentor, Subhash Embedded Classes,Bangalore.

 

VIEW SUBHASH’s PROFILE

SUBHASH LOVES TO TEACH ! CLICK HERE !

Learn from the best embedded system training institute in India.

3 Response Comments

  • Bindu mr  April 3, 2016 at 5:02 am

    This kind of explanation is what a student needs and it’s awesomely done by a quality teacher as u sir..!!! Had already learnt this in your workshop and am very happy to have a reference look here too.

    Reply
  • Vivek  April 3, 2016 at 7:08 am

    That was a fantastic and very useful explanation, we need more and more information like this.

    Reply
  • Priya Chincholikar  April 3, 2016 at 1:33 pm

    You are doing a great job with these blog posts ! Thank you :)

    Reply

Leave A Comment

Please enter your name. Please enter an valid email address. Please enter a message.