[英]Why is the value of 'i' in the loop not conflicting with the one initialized in the for loop
The value of i
is not conflicting with the value of i
initialized inside the loop, why is it so? i
i
值不冲突,为什么会这样呢?
#include <stdio.h>
int main()
{
for (int i = 0; i < 5 ; i++)
{
int i = 10;
printf("%d \t", i);
i--;
}
return 0;
}
The variable i
was initialized as zero in the loop.变量
i
在循环中被初始化为零。 Now, when the loop iteration begins, the identically named variable i
inside the braces shadows the original one (defined outside of the braces).现在,当循环迭代开始时,大括号内的同名变量
i
会隐藏原始变量(在大括号外定义)。
Since it was initialized to 10, the program displays 10
, then it is decremented by one later that doesn't affect the output because the shadowed int i = 10;
由于它被初始化为 10,程序显示
10
,然后它会减一,这不会影响 output 因为阴影int i = 10;
is executed in each iteration.在每次迭代中执行。 This will keep happening 5 times.
这将继续发生 5 次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.