Local Variable v/s Global Variable
The difference between local variable and global variable as following:
Local Variable | Global Variable |
---|---|
A variable which is defined inside a function is known as local variable. | A variable which is defined outside a function is known as global variable. |
The scope of local variable is limited to the function in which they are declared. | The scope of global variable is throughout in the porgram |
The memory is allocated to local variable each time, the control enters the function and released when the control leaves the function. |
Gloval variable remains in memory throughout the execution of the program. |
They can be used with automatic, static and register storage classes. | They can be used with only external storage class. |
Example of local variable: #include<stdio.h> int i = 10; int main() { int x = 5; printf("%d",x); } Here, x is a local variable |
Example of local variable: #include<stdio.h> int i = 10; int main() { int x = 5; printf("%d",i); } Here, i is a global variable so we can use i's value anywhere in the program. |
Table: Local variable v/s Global variable
Related Article:
No comments:
Post a Comment