Global and local scope
Write a C program to demonstrate example of global and local scope. Here’s simple program to demonstrate example of global and local scope in C Programming Language.
Scope : :
“Scope” is just a technical term for the parts of your code that have access to a variable.
-
Variables defined outside a function are […] called global variables.
-
Variables defined within a function are local variables.
A local variable is a variable which is either a variable declared within the function or is an argument passed to a function.
A global variable (DEF) is a variable which is accessible in multiple scopes. It is important to note that global variables are only accessible after they have been declared.
Below is the source code for C program to demonstrate example of global and local scope which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
/* C program to demonstrate example of 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 : :
/* C program to demonstrate example of global and local scope */ In main() a=20, b=30 In fun() a= 10 In fun() b= 40 In main() after calling fun() ~ b=30
Above is the source code for C program to demonstrate example of global and local scope which is successfully compiled and run on Windows System.The Output of the program is shown above .
If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.
Thanks for reading the post….