Variable in C Programming
Variable in C is used to give storage to a specific name. Variable are declared with a body or outside the body.
There are three types of variable used in C Programming:
- Local Variable
- Global Variable
- Static Variable
Local Variable
Local Variable gets temporary memory allocation. The scope and visibility of local variable is restricted inside the function block where it is declared. The lifetime of local variable is equal to the lifetime of function.
If we will not initialized local variable, it will produce garbage value. These variable are created when function in entered and are destroyed when the function is exited.
Example :
void func(void)
{
int a,b;
}
Above Example shows a and b are local variable which are defined within body of the func(). Local variable can be used only in those functions or block, in which they are declared. the same variable name may be used in different .
Global Variable
Variable which is declared outside the function is called as global variable. Global variable can be access by all the function which is declared after the declaration. Global variable gets permanent memory allocation. The lifetime of global variable is equal to the lifetime of program.
The Scope and visibility of global variable is throughout the program. If we will not initialized the global variable , compiler will automatically initialized it to zero.
Example :
void func1(void);
void func2(void);
int a,b=6;
int main(void)
{
printf(“Inside main() : a=%d,b=%d\n”,a,b);
func1();
func2();
return 0;
}
void func1(void)
{
printf(“Inside func1() : a=%d,b=%d\n”,a,b);
}
void func2(void)
{
int a=8;
printf(“Inside func2() : a=%d,b=%d\n”,a,b);
}
output:
Inside main: a=0,b=6
Inside func1(): a=0,b=6
Inside func2(): a=8,b=6
Here “a” and “b” are declared outside all function so they are global variable. The variable “a” will be initialized to 0 automatically. Now we can use these variable in any function. In func2(), there is local variable with in the same name as global variable . Whenever there is conflict between a local and global variable, the local variable get the precedence. so inside func2() the value of local variable get printed.
Static Variable
Static variable gets permanent memory allocation. The lifetime of static variable is equal to the lifetime of program although the scope and visibility of static variable is also restricted inside the function block. If we will not initialized the static variable compiler will automatically initialize it wo zero.
Static variable will be initialized only once in the program. static variable is useful when we want a variable which retain the value after the termination if function.
void func(void);
int main(void)
{
func();
func();
func();
return 0;
}
void func(void)
{
int a=10;
static int b=10;
printf(“a=%d, b=%d\n”,a,b);
a++;
b++;
}
Output:
a=10, b=10;
a=10, b=11;
a=10, b=12;
Above example show b is static variable. first time when the function is called b is initialized to 10. Inside the function, value of b becomes 11. This value is retained and when next time the function is called , value of b is 11 and the initialization is neglected. similarly when third time function is called , value of b is 12. Note that the variable a, which is not static is initialized on each call and its value is not retained.
Refer :- ArzaTechs