Scope of the variable in C by R4R Team

Scope of the variable is defined as the area in the program where variable is callable.
In C language scope of variable depends on the type of variable like :
1. local variable
2. global variable

Local variable:
-In case of local variable, scope is limited upto that body or function in which variable was defined.
-You can not call the variable after the function body.

Global variable:
-In case of global variable, scope is increased
-and we can excess that variable anywhere in the program.


program-

#include< stdio.h>
int a=10;
void func()
{
printf("%d",a);
//printf("%d",i); it will give an error
}
int main()
{
int i=1;
printf("%d\n",a);
printf("%d\n",i);
func();
return 0;
}


output-

10
1
10

-In this program, we define two variable i.e a=10 and i=1
-Here 'a' is global variable and 'i' is local variable.
-We print both variable inside the main() function it will not any error but if we try to access the variable 'i' inside the func() function then it will give an error.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!