Reputation:
below is my code:
//main.c
int x = 9;
int f()
{
static int x = 0;
x += 1;
return x;
}
int main()
{
printf("Value is %d", f());
return 0;
}
my questions are:
Q1-inside f()
, there is a local static varaible x
defined, and there is gloabal variable also called x
, the program does compile, but isn't it a conflict to the compiler and linker?
Q2-when I run it, the output is 1, which means that the x
in x += 1;
is the local static varaible x
, not the gloabal variable x
.But I could have mean "increment the global variable x", how can I do it?
Upvotes: 2
Views: 516
Reputation: 15042
Q1 -inside
f()
, there is a local static variablex
defined, and there is global variable also calledx
, the program does compile, but isn't it a conflict to the compiler and linker?
No, it is not a conflict. What is going on here is the proce variable shadowing. The local x
shadows the global x
.
Q2 - when I run it, the output is 1, which means that the
x
inx += 1;
is the local static variablex
, not the global variablex
. But I could have mean "increment the global variable x", how can I do it?
Declare the global variable new with extern
inside an inner scope and return from that scope:
int f (void)
{
static int x = 0;
{
extern x;
x += 1;
return x;
}
}
Note that this won't work if the global x
would be declared as static
without adding code at the global space.
Upvotes: 0
Reputation: 2065
A program can have the same name for local and global variables but the value of a local variable inside a function will take preference. There is no provision in C language to explicitly modify global variable with the same name as local inside local scope. In C++ though for accessing the global variable with same name, you'll have to use the scope resolution operator
#include <iostream>
using namespace std;
// Global variable declaration:
int g = 20;
int f()
{
int g = 0;
::g += 5;
return g;
}
int main () {
// Local variable declaration:
int g = 10;
cout << f(); // Local
cout << ::g; // Global
return 0;
}
Produces 0 25
Edit - There is indeed a way to explicitly change the global scope variable inside local scope having same variable name ( only if global variable is not declared static )
int x = 9;
int f()
{
static int x = 0;
{
extern int x;
x += 1;
}
return x;
}
int main()
{
printf("Value is %d", f());
return 0;
}
Produces 0
Upvotes: 4
Reputation: 234705
Once conceptually, program control reaches the local declaration of x
in f
, the global x
is shadowed. No syntax exists in C for accessing the global x
.
Other than renaming one of the variables,
int f()
{
int* y = &x; // still refers to the global x
static int x = 0;
x += 1;
*y += 1; // increments the global x via the pointer
return x;
}
is an option, although not particularly sensible.
Upvotes: 1
Reputation: 213832
Q1. The language was intentionally designed this way so that we don't have to worry about what identifiers that reside in the global namespace - there can be quite a lot of them in large projects.
Internally the compiler doesn't use variable names at all, but sometimes their names must preserved for the purpose of generating debugger files (like ELF etc). In such situations, the compiler uses something called "name mangling" to keep the two variables separate internally. For example if I disassemble your code with gcc x86 and no optimizations, it names the local one x.0
internally, where the .0
thing is a compiler-specific addition, not to be confused with valid C syntax.
Q2. From inside the function f
where the local x
is visible, you can't. Unless you explicitly pass along a pointer to the other variable through a parameter etc. Again, we wouldn't want to change file scope variable values by accident.
Upvotes: 0
Reputation: 17668
Q1-inside f(), there is a local static varaible x defined, and there is gloabal variable also called x, isn't it a conflict to the compiler and linker?
No, the function always treats the local variable name first. The local name x
in function f
shadows the global name x
.
Q2-when I run it, the output is 1, which means that the x in x += 1; is the local static varaible x, not the gloabal variable x.But I could have mean "increment the global variable x", how can I do it?
It's the same as your first question. You call f()
which uses the local variable name x
, which shadows the global name x
. If you want the global x
, you need to use it directly, not via function f
.
Upvotes: 1