Variable Types:

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.

variable-types

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.

 

Variable Declaration

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.

Syntax:

Data_type variable name;

EXAMPLE: int marks;

 

Variable definition

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.

 

Variable initialization

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.

 

Rules for naming the variables

Following are some rules for naming a variable:

  1. Variables may include letters, numbers and underscore.
  2. The must character must be a letter or underscore.
  3. Blank spaces are not allowed.
  4. Special symbols cannot be used.
  5. Reserved words are not allowed.
  6. A variable can be up to 31 characters long.
  7. Both upper and lower case letters are allowed.

 

Lvalues and Rvalues

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.