[英]Tracking global variables with the same name as local variables in C
I'm having a few issues tracking the values of global variables when there are local variables that exist with the same name. 当存在具有相同名称的局部变量时,我在跟踪全局变量的值时遇到了一些问题。
This is the code I'm working with: 这是我正在使用的代码:
#include <stdio.h>
void func(int);
int x=6, y=7, z=10;
int
main(int argc, char **argv) {
int z=5;
printf("main: x=%2d, y=%2d, z=%2d\n", x, y, z);
func(x);
printf("main: x=%2d, y=%2d, z=%2d\n", x, y, z);
func(y);
printf("main: x=%2d, y=%2d, z=%2d\n", x, y, z);
func(z);
printf("main: x=%2d, y=%2d, z=%2d\n", x, y, z);
return 0;
}
void
func(int x) {
x = x+1;
y = y+1;
printf("func: x=%2d, y=%2d, z=%2d\n", x, y, z);
}
Note the globals have the same name as the locals in func(), except for z. 请注意,全局变量与func()中的局部变量具有相同的名称,但z除外。 When I run the program.
当我运行程序时。 I get the following output:
我得到以下输出:
main: x= 6, y= 7, z= 5
func: x= 7, y= 8, z=10
main: x= 6, y= 8, z= 5
func: x= 9, y= 9, z=10
main: x= 6, y= 9, z= 5
func: x= 6, y=10, z=10
main: x= 6, y=10, z= 5
I can understand where the first line comes from. 我可以理解第一行的来源。 It's just the values of the global variables, but main is using 5 rather than 10 because the global variable shadows the local variable.
它只是全局变量的值,但main使用5而不是10,因为全局变量遮盖了局部变量。 I can also understand line 2. global variable x is passed into func, giving a 7. Global variable y is used also, giving an 8.
我也可以理解第2行。将全局变量x传递给func,得到7。还使用全局变量y,得到8。
Line 3 is where I lose track of values. 第3行是我丢失值的地方。 Why has the value of global variable y stayed as 8?
为什么全局变量y的值保持为8? Did the func() function call change its value permanently?
func()函数调用是否永久更改其值? I thought this was not possible since y in func() is just a local variable.
我认为这是不可能的,因为func()中的y只是一个局部变量。 I understand where the x = 6 comes from in line 3 though.
我知道x = 6来自第3行的地方。
Line 4 I lose track even more. 4号线我走得更远。 How can the value of x jump from 6 to 9?
x的值如何从6跳到9?
It would be great if someone could walk me through this output, and perhaps give me a quick explanation on scope and also on shadowing. 如果有人可以指导我完成此输出,然后也许对我的范围和阴影给出一个快速的解释,那将是很好的。
To answer your first question 回答您的第一个问题
Why has the value of global variable y stayed as 8?
为什么全局变量y的值保持为8? Did the func() function call change its value permanently?
func()函数调用是否永久更改其值?
Yes it did change the global variable y
permanently, because it is global and not local (it would be local if it was declared as int y;
) 是的,它确实永久更改了全局变量
y
,因为它是全局变量,而不是局部变量(如果声明为int y;
它将是局部变量)。
How can the value of x jump from 6 to 9?
x的值如何从6跳到9?
You pass the global variable y
, which is changed to 8 in the first function call, to the function. 您将全局变量
y
(在第一个函数调用中更改为8)传递给该函数。 This value is locally stored in the function as x
. 此值在本地存储为
x
。 So x + 1 will become 9. 因此x +1将变为9。
This is the danger of global variables. 这是全局变量的危险。 Once you name local variables the same you WILL lose track of what value is contains what value.
将局部变量命名为相同的变量后,您将无法跟踪什么值包含什么值。
A little example to explain some details: 一个小例子来解释一些细节:
int x = 3;
int main(){
int x = 8;
printf("%d", x);
}
This will print 8
because the code will grab the local value of x
. 这将打印
8
因为代码将获取x
的本地值。
Furthermore, when you change a global variable this will be remembered for the lifetime of the program. 此外,当您更改全局变量时,将在程序生命周期内记住该变量。
y
is not a local variable inside func()
. y
不是func()
内的局部变量。 In order for it to be a local variable (which would shadow the global y
variable), you would have to declare another y
variable inside func()
, eg as in the example below. 为了使其成为局部变量(它将遮盖全局
y
变量),您将必须在func()
内部声明另一个y
变量,例如下面的示例。 (This version will always print "y=1", and will not modify the global y
variable.) (此版本将始终打印“ y = 1”,并且不会修改全局
y
变量。)
void
func(int x) {
int y = 0;
x = x+1;
y = y+1;
printf("func: x=%2d, y=%2d, z=%2d\n", x, y, z);
}
Since y
is not a local variable inside the original func()
, it's incrementing the global y
. 由于
y
不是原始func()
内部的局部变量,因此它将使全局y
递增。
Note that x
is local to func()
too, and is passed in by value. 请注意,
x
也是func()
局部变量,并按值传递。 That is, a call func(foo)
will copy the value of foo
into the x
parameter, and changing the x
parameter inside func()
will not change foo
. 也就是说,调用
func(foo)
将的值复制foo
成x
参数,并改变x
内部参数func()
不会改变foo
。
Line 4 comes from the func(y)
call. 第4行来自
func(y)
调用。 It will set the (local) x
parameter inside func()
to the current value of the global variable y
. 它将
func()
的(local) x
参数设置为全局变量y
的当前值。 y
happens to have the value 8 at that point. y
恰好在那时具有值8。 (It was incremented from 7 to 8 by the previous call to func()
.) The assignment x = x+1
inside func()
bumps that to 9, which is then printed. (它是从7中由先前调用递增到8
func()
赋值x = x+1
内func()
凸点,为9,然后将其打印出来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.