VARIABLES IN C


Types:

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

Local Variable:

A variable that is declared inside the function or block is called a local variable. It must be declared at the start of the block.

Example:

void function(){

int x=10;//local variable

}

Global Variable:

A variable that is declared outside the function or block is called a global variable. Any function can change the value of the global variable. It is available to all the functions. It must be declared at the start of the block.

Example:

void function(){

int x=10;//local variable

}

Static Variable:

A variable that is declared with the static keyword is called static variable. It retains its value between multiple function calls.

Example:

void function(){

int x=10;//local variable

}

Automatic Variable:

All variables in C that are declared inside the block, are automatic variables by default. We can explicitly declare an automatic variable using auto keyword

Example:

void main(){

int x=10;//local variable (also automatic)

auto int y=20;//automatic variable }

External Variable:

We can share a variable in multiple C source files by using an external variable. To declare an external variable, you need to use extern keyword

Example:

extern int x=10;//external variable (also global)