Types of Variables

There are five types of variables in C :

1. Local variable
2. Global variable
3. Static variable
4. Automatic variable
5. External variable

1. Local variable : 

A variable that is declared inside the function or block is called local variable. A variable declared within a function has a local scope and can only be referenced within that function. Any assignment outside of that function will be considered to be an entirely different variable from the one contained in the function. For example :
#include <stdio.h>

void Myfunc() {
  int x = 20;  // local variable
  printf("Myfunc [x] : %d\n", x);
}

int main() {
  int x = 40;
  Myfunc();
  printf("Main [x] : %d\n", x);
  return 0;
}
Output :

Myfunc [x] : 20
Main [x] : 40

At the above example variable is local inside the function Myfunc() and also inside main function.

2. Global variable :

A variable that is declared outside the function or block is called global variable. Any function can change the value of the global variable. It is available to all the functions. Example :
#include <stdio.h>

int x = 20;

void Myfunc() {
  x = 50;
  printf("Myfunc [x] : %d\n", x);
}

int main() {
  Myfunc();
  printf("Main [x] : %d\n", x);
  return 0;
}
Output :

Myfunc [x] : 50
Main [x] : 50

As we can see that the value of x is modified by the Myfunc() variable.

3. Static Variable :

A static variable is a variable that has been allocated statically, means its lifetime is the entire run of the program. Generally the variables declared inside the functions are destroyed or deleted when function ends. However sometimes we need those variables for some reason. So to this we have to use the keyword static when defining the variable. The syntax for creating a static variable is :
static datatype variable_name;
Example :
static int x = 10;
Now lets see an Example :
#include <stdio.h>

void Myfunc() {
  static int x = 1;
  printf("%x\n", x);
  x++;
}

int main() {
  Myfunc();
  Myfunc();
  Myfunc();
 
  return 0;
}
Output :

1
2
3

At the above example we can see that the static variable in MyFunc() will not destroyed at end of function, instead it increament itself and print the old value again in next call.

4. Automatic variable :

An automatic variable is a local variable which is allocated and de-allocated automatically when program flow enters and leaves the variable's scope. All variables in C that is declared inside the block, are by default automatic variables. But we can also declare automatic variables by using auto keyword. for example :
auto variable_name = value;
Example :
#include <stdio.h>

void Myfunc() {
  auto int x = 20;
  printf("%d\n", x);
}

int main() {
  Myfunc();
}
Output :

20

5. External variable :

An external variable is a variable defined outside any function block. By using extern keyword, we can create external variable, which We can share it with multiple C source files. The syntax is :
extern variable_name = value;
Example :

Filename : myheader.h
extern int num = 10;
Filename : test.c
#include <stdio.h>
#include "myheader.h"

int main() {
  printf("Global variable is : %d\n", num);
}
Output :

Global variable is : 10


Next Topic :