Suppose you have a global variable name i
and you have two functions function1 and function2. In both functions, you printed the value of i
. In function1 you have declared the value of i
again. So in function1 you have a local variable i
.
#include<stdio.h>
int i = 10;
void function1()
{
int i = 20;
printf("%d\n", i);
}
void function2()
{
printf("%d", i);
}
int main()
{
function1();
function2();
return 0;
}
The compiler will consider the local variable i
in function1 and print 20
. On the other hand in the function2, there is no local variable named i
. So it will print the global variable i = 10
.