C variable is a named location in a memory where a program can manipulate the data. This location is used to hold the value of the variable. The value of the C variable may get change in the program. C variable might be belonging to any of the data type like int, float, char etc.
When we want to store any information(data) on our computer/laptop, we store it in the computer's memory space. Instead of remembering the complex address of that memory space where we have stored our data, our operating system provides us with an option to create folders, name them, so that it becomes easier for us to find it and access it.
Similarly, in programming language, when we want to use some data value in our program, we can store it in a memory space and name the memory space so that it becomes easier to access it.
A variable in simple terms is a storage place which has some memory allocated to it. Basically, a variable used to store some form of data. Different types of variables require different amounts of memory, and have some specific set of operations which can be applied on them.
Declaration of variables must be done before they are used in the program. Declaration does the following things.
Until the variable is defined the compiler doesn't have to worry about allocating memory space to the variable.
Example of only declaration
extern int a;
extern float b;
extern double c, d;
Defining a variable means the compiler has to now assign a storage to the variable because it will be used in the program
Example of declation with definition
int a;
float b, c;
Variable declaration refers to the part where a variable is first declared or introduced before its first use. Variable definition is the part where the variable is assigned a memory location and a value. Most of the times, variable declaration and definition are done together.
Only with the help of extern keyword declaration and definition of variable separetely possible.
Variable declaration | Variable definition |
---|---|
Declaration tells the compiler about data type and size of the variable. | Definition allocates memory for the variable. |
Variable can be declared many times in a program. | It can happen only one time for a variable in a program. |
The assignment of properties and identification to a variable. | Assignments of storage space to a variable. |
Initializing a variable means to provide it with a value. A variable can be initialized and defined in a single statement, like:
int a = 10;
Identifier | Variable |
---|---|
Identifier is the name given to a variable, function etc. | While, variable is used to name a memory location which stores data. |
An identifier can be a variable, but not all indentifiers are variables. | All variable names are identifiers. |
Example: // a variable int studytonight; // or, a function int studytonight() { .. } |
Example: // int variable int a; // float variable float a; |