Difference between Definition and Declaration in C
Part 5 of Complete C Course
Foreword: This tutorial explains the difference between Definition and Declaration in C.
By: Chrysanthus Date Published: 31 May 2024
Introduction
Using int
The following statement is a declaration and not a definition:
int myInt;
Four bytes for any practical int value is allocated in memory, and the region for the allocation is identified by the identifier, myInt. The following two statements are declaration and definition respectively:
int myInt; //declaration
myInt = 5; //definition or initialization
The second statement is not preceded with the object type, int, because that has already been done in the declaration (first statement). The second statement assigns a practical value to myInt. This is initialization as well as it is definition. The practical (useful) value of 5 is placed in the location, identified by myInt. Accept the fact that 5 as an integer, occupies 4 bytes and not 1 byte, in the region (location) identified by myInt.
The following single combined statement can be considered as definition or declaration or initialization:
int myInt = 5;
Using char
The following statement is a declaration and not a definition:
char ch;
One byte for any practical char (character) value is allocated in memory, and the region for the allocation is identified by the identifier, ch. The following two statements are declaration and definition respectively:
char ch; //declaration
myInt = 'E'; //definition or initialization
The second statement is not preceded with the object type, char, because that has already been done in the declaration (first statement). The second statement assigns a practical value to ch. This is initialization as well as it is definition. The practical (useful) value of E is placed in the location, identified by ch. Accept the fact that E as a char (character), occupies 1 byte in the region (location) identified by ch.
The following single combined statement can be considered as definition or declaration or initialization:
char ch = 'E';
Conclusion
Definition means having a practical value for the identifier. Declaration can mean definition or it can mean just having the identifier with memory allocated to it, without a practical (useful) value.
At this point it is nice to give the difference between region and location: A region can consist of one or more consecutive locations.