#include <stdio.h>
int g;
void afunc(int x)
{
g = x; /* this sets the global to whatever x is */
}
int main(void)
{
g = 10; /* global g is now 10 */
afunc(20); /* but this function will set it to 20 */
printf("%d\n", g); /* so this will print "20" */
return 0;
}
The output of printf is 20.
but the local variable g = 10,
so why it is printing 20 instead of 10
does local variable has more scope than global variable ?
Because it doesn’t appear that you actually declared a new variable. You just referred to
g = 10;
You didn’t actually define a new variable, simply referenced a global one. Hope this helps.