Preview
Scope of variable in C Language | Local variable in C Language | Global variable in C Language
Scope is the lifetime of variables. Variables have two types depending upon scope.
- Local variable
- Global variable
a. Local variable in C Language
Variable declared inside a function body is called local variable. It can exist only within the function in which it is declared.
b. Global variable in C Language
Variable declared before all function definitions is known as global variable. The value of global variable exists throughout the program. Changes made to them have effect on entire .
Program for local and global variables in C. |
#include<stdio.h> int k=10; /*global variable*/ void show() { printf(“\nGlobal variable within show() %d”,k); k++; } int main() { int k=50; /*Local variable*/ printf(“Local variable in main=%d”,k); printf(“Global variable in main()=%d”,k); ++k; show(); printf(“Global variable in main()=%d”,k); return(0); } |
Output |
Local variable in main=50 Global variable in main() =10 Global variable in function a=11 Global variable in main() a=12 |
Popular Books of Computer Science