Google News
logo
Embedded C - Interview Questions
What is the difference between declaration and definition of a variable?
Declaration of a variable in C : 
A variable declaration only provides sureness to the compiler at the compile time that variable exists with the given type and name, so that compiler proceeds for further compilation without needing all detail of this variable. When we declare a variable in C language, we only give the information to the compiler, but there is no memory reserve for it. It is only a reference, through which we only assure the compiler that this variable may be defined within the function or outside of the function.
 
Note : We can declare a variable multiple times but defined only once.
Ex : 
extern int data;
extern int foo(int, int);
int fun(int, char); // extern can be omitted for function declarations
 
Definition of variable in C :
The definition is action to allocate storage to the variable. In another word, we can say that variable definition is the way to say the compiler where and how much to create the storage for the variable generally definition and declaration occur at the same time but not almost.
 
Ex : 
int data;
int foo(int, int) { }
Note : When you define a variable then there is no need to declare it but vice versa is not applicable.
Advertisement