![]() |
VOOZH | about |
A variable in C is a named piece of memory which is used to store data and access it whenever required.
Age: 20 Height: 5.7 Grade: A
We can assign any name to a C variable as long as it follows the following rules:
Age: 20 Height: 5.7 Grade: A
Note: Values should be type-compatible, otherwise C performs implicit or explicit type conversion.
The data stored inside a C variable can be easily accessed by using the variable's name.
3
We can also update the value of a variable with a new value whenever needed by using the assignment operator =.
Initial value: 10 Updated value: 25 After adding 5: 30
Variables act as name for memory locations that stores some value. It is valid to use the variable wherever it is valid to use its value. It means that a variable name can be used anywhere as a substitute in place of the value it stores.
60 70
When a variable is declared, the compiler is told that the variable with the given name and type exists in the program. But no memory is allocated to it yet. Memory is allocated when the variable is defined.
Most programming languages like C generally declare and define a variable in the single step. For example, in the above part where we create a variable, variable is declared and defined in a single statement.
The size of memory assigned for variables depends on the type of variable. We can check the size of the variables using sizeof operator.
4 bytes
Variables are also stored in different parts of the memory based on their storage classes.