Home »
C solved programs »
C basic programs
C program to demonstrate example of global and local scope
Variable and function which are declared in the global scope can be accessed anywhere in the program, while variable and functions which are declared within the local scope can be accessed in the same block (scope).
Problem statement
This program will demonstrate the example of global and local scope in c programming language. These scopes are used according to the requirement.
C program to demonstrate example of global and local scope
/*C program to demonstrate example global and local scope. */
#include <stdio.h>
int a=10; //global variable
void fun(void);
int main()
{
int a=20; /*local to main*/
int b=30; /*local to main*/
printf("In main() a=%d, b=%d\n",a,b);
fun();
printf("In main() after calling fun() ~ b=%d\n",b);
return 0;
}
void fun(void)
{
int b=40; /*local to fun*/
printf("In fun() a= %d\n", a);
printf("In fun() b= %d\n", b);
}
Output
In main() a=20, b=30
In fun() a= 10
In fun() b= 40
In main() after calling fun() ~ b=30
C Basic Programs »