There are two type of Declaration for variable, depending upon type and place of declaration we call it
1. Global variable
2. Local Variable
Global variable is a variable which is declared outside of any function inside that program. The special thing is that these variables can be accessed from any were. Values are inserted in these variables can globally accessed.
Local variables is a variable which is declared inside of any function. Here the value of that variable is only accessed by that function.
Now let’s see the example
Here “sum” is a local variable. It only can accessed by function “add()”. If we try to call “sum” variable from “main()” function then it will cause errors.
Now let’s see the output of this program
1. Global variable
2. Local Variable
Global variable is a variable which is declared outside of any function inside that program. The special thing is that these variables can be accessed from any were. Values are inserted in these variables can globally accessed.
Local variables is a variable which is declared inside of any function. Here the value of that variable is only accessed by that function.
Now let’s see the example
#include<stdio.h>
int a=12;
int b=10;
void main()
{
printf("value of a %d\n",a);
printf("value of b %d\n",b);
add();
}
int add()
{
int sum;
sum=a+b;
printf("The Value of Sum is %d",sum);
}
Here “a” and “b” are global variables. They can access by any function, for example math operation done on “a” and “b” inside “add()” function also value of “a” and “b” printed from “main()” function.int a=12;
int b=10;
void main()
{
printf("value of a %d\n",a);
printf("value of b %d\n",b);
add();
}
int add()
{
int sum;
sum=a+b;
printf("The Value of Sum is %d",sum);
}
Here “sum” is a local variable. It only can accessed by function “add()”. If we try to call “sum” variable from “main()” function then it will cause errors.
Now let’s see the output of this program
value of a 12
value of b 10
The Value of Sum is 22
value of b 10
The Value of Sum is 22

0 comments:
Post a Comment