Declaration and Definition of local variables

Mon Nov 7, 2022


Intricacies of C language

"I think the world is run by "C" students" — Al McGuire

The question is often asked in the interview or viva-voce. Here I am going to discuss as far as variable declaration and definiton is concerned. Here I am going to discuss only local variable storage class.

What is a Definition in C
The definition of a variable informs the compiler where and how much storage to create for the variable. A variable definition defines the data type and a set of one or more variables of that type.
For example

int x;

It declares and defines the variables a. It tells the compiler to create an integer variable having name x.

Initialization is the process of assigning initial values to the variables at the time of declaration.

int x = 5;                                  // declaration and definition as well as initialization

On the contrary
int x; // declaration and defintion
x = 5;                                      // assignment

Difference Between Declaration and Definition in C

The declaration is a statement that assures the compiler of the existing variable so that the compiler can proceed for further compilation without requiring the complete details about the variable. On the other hand, the definition is a statement that explains the compiler on where and how much storage to create for the variable. Thus, this is the main difference between Declaration and Definition in C. However you would understand complete meaning of the samewhen I post next article.

Bhalchandra Gholkar
A C and C++ language lover