Local Variable
A variable declared inside a function is known as local variable. Local variables are also called automatic variables.
Syntax
auto data_type identifier
Scope The area where a variable can be accessed is known as scope of variable.
Scope of local variable
Local variable can be used only in the function in which it is declared.
Lifetime
The time period for which a variable exists in the memory is known as lifetime of variable.
Lifetime of local variable
Lifetime of local variables starts when control enters the function in which it is declared and it is destroyed when control exists from the function.
Global variable
A variable declared outside any function is known as global variable. Global variables can be used by all functions in the program.
Scope of Global variable
These variables are globally accessed from any part of the program. They are declared before main function.
Lifetime of Global variable
Global variables exist in the memory as long as the program is running. These variables are destroyed from the memory when the program terminates. These variables occupy memory longer than local variables.
Syntax
auto data_type identifier
Scope The area where a variable can be accessed is known as scope of variable.
Scope of local variable
Local variable can be used only in the function in which it is declared.
Lifetime
The time period for which a variable exists in the memory is known as lifetime of variable.
Lifetime of local variable
Lifetime of local variables starts when control enters the function in which it is declared and it is destroyed when control exists from the function.
Global variable
A variable declared outside any function is known as global variable. Global variables can be used by all functions in the program.
Scope of Global variable
These variables are globally accessed from any part of the program. They are declared before main function.
Lifetime of Global variable
Global variables exist in the memory as long as the program is running. These variables are destroyed from the memory when the program terminates. These variables occupy memory longer than local variables.