A variable is a named memory location or memory cell. It is used to store program’s input data and its computational results during execution. The value of variable may be changed during the execution of program. However, the name of the variable cannot be changed.
The variables are created in RAM. RAM is a temporary memory. That is why the data stored is also temporary. It can only be used and processed during the execution of program. The data stored in the variable is automatically removed when program ends.
Name of variable: It refers to an identifier that represents a memory location.
Address of variable: It refers to the memory location of the variable.
Contents of variable: It refers to the value stored in memory location referred by variable.
The process of specifying the name of the variable and its data type is called variable declaration. A program can have as many variables as needed. C is a strongly typed language. It means that all the variables must be declared before their use in the program.
The variable declaration provides information to the compiler about the variable. The compiler uses the declaration to determine how much memory is needed for each variable. Different types of data require different amount of memory. The memory is allocated when the variable is declared.
Once the variable is declared its data type cannot be changed during the program execution. However the value can be changed.
Data_type variable name;
EXAMPLE: int marks;
Generally there is a slight difference between declaring and defining a variable. The variable declaration specifies only the name of the variable and its data type but definition of variable specifies the name, data type of variable as well as allocates the memory location.
Declaration and definition of variables are not specified separately. The variable definition is automatically performed when a variable is declared.
The process of assigning a value to a variable at the time of declaration is known as variable initialization.
It is discussed earlier that the compiler automatically allocates the required memory for the variable when it is declared. This memory location may contain some meaningless data. This meaningless data is call garbage value. It may produce unexpected results in computation. To avoid this are variables are initialized.
Following are some rules for naming a variable:
- Variables may include letters, numbers and underscore.
- The must character must be a letter or underscore.
- Blank spaces are not allowed.
- Special symbols cannot be used.
- Reserved words are not allowed.
- A variable can be up to 31 characters long.
- Both upper and lower case letters are allowed.
An lvalue is an operand that can be written on the left side of assignment operator =. It must always be a single value.
An rvalue is an operand that can be written on the right side of assignment operator =. All lvalues can be used as rvalues but all rvalues cannot be used as lvalue.
The expression x=5 is valid but the expression 5=x is invalid.