A variable in C language is the name associated with some memory location to store data of different types. There are many types of variables in C depending on the scope, storage class, lifetime, type of data they store, etc. A variable is the basic building block of a C program that can be used in expressions as a substitute in place of the value it stores.
They can be viewed as the names given to the memory location so that we can refer to it without having to memorize the memory address. The size of the variable depends upon the data type it stores.
There are 3 aspects of defining a variable:
- Variable Declaration
- Variable Definition
- Variable Initialization
1. C Variable Declaration
Variable declaration in C tells the compiler about the existence of the variable with the given name and data type. When the variable is declared compiler automatically allocates the memory for it.
2. C Variable Definition
In the definition of a C variable, the compiler allocates some memory and some value to it. A defined variable will contain some random garbage value till it is not initialized.
Example ;
int var;
char var2;
Note: Most of the modern C compilers declare and define the variable in single step. Although we can declare a variable in C by using extern keyword, it is not required in most of the cases. To know more about variable declaration and definition, click here.
3. C Variable Initialization
Initialization of a variable is the process where the user assigns some meaningful value to the variable.
Example ;
int var; // variable definition
var = 10; // initialization
or
int var = 10; // variable declaration and definition- Rules for Naming Variables in C
You can assign any name to the variable as long as it follows the following rules:
- A variable name must only contain alphabets, digits, and underscore.
- A variable name must start with an alphabet or an underscore only. It cannot start with a digit.
- No whitespace is allowed within the variable name.
- A variable name must not be any reserved word or keyword.
- C Variable Types
The C variables can be classified into the following types:
- Local Variables
- Global Variables
- Static Variables
- Automatic Variables
- Extern Variables
- Register Variables


