# Variables and types of variables

In very simple terms a variable is a location in memory which has an address and a name. It can be used to store data. Each variable is of a given data type. From its data type we come to know what kind of values we can store in that variable.

## How to declare a variable

In any program you have to first declare a variable and then use it. This is like saying that you have to first ask the computer to assign you a memory in the RAM so that you can store your data at the memory location. You declare a variable like this

datatype  variable_name ;

All variables have a data type which is written in front of the variable name.

You can give your variable any name but:

1. You cannot put space in between variable names
2. You cannot start a variable name with a number or special character

## Types of variables

There are 3 places where you can declare variables and therefore there are 3 types of variables

### 1) As function parameters - Formal parameters

While declaring a function you can provide parameters for argument passing. For example

int Sum ( int a , int b) // a and b are called formal parameters  {      return a + b;  }

This function has two variables declared as parameters , a and b , and both are of the type int. They can be used as normal variables and their scope is only inside the body of the function.

### 2) Inside a code block - Local variables

You can declare variables inside a code block and they are called local variables. A code block starts with opening curly bracket { and ends with closing curly bracket } . They can only be used inside the code block where they are declared. Their life begins when the program execution enters the code block and their life ends when the program execution leaves the code block.

For example

int Sum (int a , int b) // a and b are formal parameters  {      int x , y;  // x and y are local variables.                     //They are inside the Sum function code block.      x = a + 5;      y = b + 10;        if( x > 20)      {          int x = a + 21;            // x is a local variable inside the if block of code.           //This x is different from the x declared inside the body of the sum function.          printf(" %d ", x);      }        return x + y;  }

In the function above x and y are declared inside the sum function and therefore can be called as local variables. Also we have declared another x inside the if code block. This is also called local variable. But its life is only inside the if code block and it is different from the x declared inside the sum function.

Local variables get their life and hold value only till the code execution is inside their code block. When the code execution moves out of their code block the values of local variables are lost.

Local variables are also called automatic variables and you can put the keyword auto while declaring them. But as all local variables are auto by default, this keyword is never used.

### 3) Outside the body of the function - Global variables

Variables declared outside the body of the function are called global variable. They can be used throughout the program and hold their value throughout the program execution.

For example

int x , y;  // x and y are global variables  int Sum ( int a , int b)  {      x = a + 5;      y = b + 10;      return x + y;  }

In this code x and y are global variables.

If you declared local variables with the same name as global variables then all the references made to the variable inside the code block will refer to the local variable inside that code block.

### E-Magazines

@

Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization