#include<stdio.h>
int x = 12;
void foo(int x)
{
printf("%d\n",x);
}
int main()
{
foo(3);
printf("%d\n",x);
return 0;
}
This gives output
3
12
How can i access global int x inside the function foo()?
#include<stdio.h>
int x = 12;
void foo(int x)
{
printf("%d\n",x);
}
int main()
{
foo(3);
printf("%d\n",x);
return 0;
}
This gives output
3
12
How can i access global int x inside the function foo()?
You can, but the parameter shadows it. Just use another name for parameter variable in foo
function.
Yes it works like this as variables of same names are allowed provided that they are in different scopes. "x" is both defined in global scope (global variable) and a local scope (in foo() as a parameter). But they are located in different locations in memory.