简体   繁体   English

解释给定代码的输出

[英]Explain the output of the given code

In the below code, variable i has been declared globally as well as locally in the for loop . 在以下代码中,变量i已在for循环中全局以及局部声明。 Due to high precedence of local variable, i will be initialized with the value 10 . 由于局部变量的优先级较高,因此我将使用值10进行初始化。 But in the first occurrence of the loop, due to i++in the for loop, value of i will become 11, so should it not exit the loop after the first instance only? 但是在第一次出现循环时,由于for循环中的i ++,i的值将变为11,那么它是否不应仅在第一个实例之后退出循环?

#include<stdio.h>
int main(){
int i;
for(i=0;i<5;i++){
     int i=10;
     printf(" %d",i);
     i++;
}
return 0;

} }

PS: The answer is 10 10 10 10 10 PS:答案是10 10 10 10 10

Precedence is not just about initialization. 优先级不仅与初始化有关。 The variable i inside the loop body is a different variable from the one outside. 循环体内的变量i与外部的变量不同 It "shadows" the outer i , making it inaccessible in the loop body. 它“遮盖”了外部i ,从而使其在循环主体中无法访问。

The loop control statement is outside the loop body. 循环控制语句在循环主体之外。 Its i is the outer i , and nothing done to the inner i has any effect on it, so it starts at 0 and counts up to 5. At each loop iteration, the inner i is re-initialized to 10, and that's the one that gets printed. 它的i是外部i ,对内部i所做的任何操作都不会对其产生任何影响,因此它从0开始并计数到5。在每次循环迭代中,内部i被重新初始化为10,也就是一个被打印出来。

The reason it always prints is because the inner i shadows the outer i . 它总是打印的原因是因为内部i 遮蔽了外部i For the same reason the i++; 出于同样的原因, i++; inside the loop increments the inner i , not the i that's used as the for loop counter. 循环内部将增加内部i ,而不是用作for循环计数器的i

But the i that controls the loop gets incremented after the scope of the inner i is over and has no relation to i++; 但是,控制循环的i在内部i的作用域结束后会增加,并且与i++;没有关系i++; done inside the loop. 在循环内完成。 It's completely independent of the inner i and thus the loop runs 5 times. 它完全独立于内部i ,因此循环运行5次。

GCC provides an option to warn about such shadowing: -Wshadow . GCC提供了一个警告此类阴影的选项: -Wshadow

Yet another answer because there is one aspect missing so far. 还有一个答案,因为到目前为止还缺少一个方面。 If you change your code slightly: 如果您稍微更改代码:

#include<stdio.h>     
int main(){
int i;
for(i=0;i<5;i++){
     static int i=10;
     printf(" %d",i);
     i++;
}
return 0;
}

You will get a more "expected" result: 您将获得更多“预期”结果:

10 11 12 13 14 10 11 12 13 14

A variable inside a block stops to exist after the block (read: pair of curly braces) is left (and it is left here after every step to execute the control statements of for ) ... so with your original code, you get a new inner i each time. 离开该块(读取:一对大括号)后(在执行for的控制语句的每一步之后都留在这里),该块内的变量不再存在...因此,使用原始代码,您会得到一个每次i新的内心 Declaring the inner i static still doesn't change the scope (it is only visible inside the loop body), but makes the variable survive and still be available when the same block is entered again. 声明内i static依然不改范围(这仅仅是循环体中可见),但使得可变生存 ,还是当再次进入同一个块是可用的。

Yes the answer is correct. 是的,答案是正确的。

As loop iterates 5 times and for every iteration i inside loop is set to 10 and outer one is overshadowed by inner one. 当循环迭代5次时,对于每次迭代, i内部循环设置为10而外部循环则被内部循环遮盖。 After i=10 its value is printed therefore 5 times 10 is output. i=10之后,将打印其值,因此输出5乘以10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM