Variable Declaration vs definition : An illustration
Variable Declaration
A declaration introduces the existence and type of a variable to the compiler. It tells the compiler that a variable with a specific name and data type will be used in the program. A declaration does not allocate memory for the variable; it simply informs the compiler about its existence.
Variable Definition
A definition not only declares the variable but also allocates memory for it. It associates the name of the variable with a specific location in memory where the variable’s value can be stored.
Difference between a variable Declaration and definition in C
- Variable is defined when the compiler generates instructions to allocate the storage for the variable.
- A variable is declared when the compiler is informed that a variable exists along with its type. The compiler does not generate any instructions to allocate the storage for the variable at that point.
- A variable definition is also a declaration, but not all variable declarations are definitions.
int myExamScore;
After that, a value to this variable like myExamScore = 540;
Here, you should note that this variable is defined in the local space. Because, that is local to the main function.
You can also define a variable in the global space. Global space is common to all the functions of the program. So, when we discuss the scope of a variable, we’ll discuss global and local space.
For example, I can also put int myExamScore variable definition in the global space area.
#include<stdio.h> int myExamScore; int main() { myExamScore = 540; printf("Hello World"); return 0; }
Variable definition
So, this also compiles fine, no problem. And here, int myExamScore is also called variable definition in the global space.
If I use one keyword called ‘extern‘.
‘extern‘ is a standard keyword. This is a reserved keyword of the ‘C’ standard.
If I compile this program, it gives an error; the error is an undefined reference made to this variable.
#include<stdio.h> extern int myExamScore; int main() { myExamScore = 540; printf("Hello World"); return 0; }
Variable declaration
extern int myExamScore is a variable declaration. Here, why is it called a variable declaration? Because when the compiler sees this, it will not allocate any storage for this variable. So here, you are telling the compiler by this keyword that this variable is defined somewhere in other files of the project. That is outside of this main.c file. So, that is the meaning of using the extern keyword. That’s why the compiler will not allocate any storage for this. So, it will assume that it is defined somewhere else in the program.
When it tries to generate instructions to put this value 540 into the memory location in the computer memory, it will have a problem because you have not defined that variable. That’s why finally, the compiler says that there is an undefined reference to this variable.
Basically, the compiler is saying that it couldn’t able to find the reference to that variable. You said that it is an extern, but the compiler couldn’t able to find that.
So, that’s the difference between variable definition and declaration.
Get the Microcontroller Embedded C Programming Full Course on Here.
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1