For-Loop 计数器是否保留?

发布于 2024-10-05 05:03:52 字数 216 浏览 0 评论 0原文

简单的问题。想象一下 ANSI-C 中的情况:

int i;

for(i=0 ; i<5 ; i++){
   //Something...
}

printf("i is %d\n", i);

这会输出“i is 5”吗?

循环后,i 是否保留,或者 i 的值是否未定义?

Simple Question. Imagine this in ANSI-C:

int i;

for(i=0 ; i<5 ; i++){
   //Something...
}

printf("i is %d\n", i);

Will this output "i is 5" ?

Is i preserved or is the value of i undefined after the loop?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

百合的盛世恋 2024-10-12 05:03:52

是的。如果 i 在 for 循环之外声明,则在循环退出后它仍保留在范围内。它保留循环退出时的任何值。

如果您在循环中声明 I:

for (int i = 0 ; i < 5 ; i++)
{

}

则循环退出后 i 是未定义的。

Yes. If i is declared outside of the for loop it remains in scope after the loop exits. It retains whatever value it had at the point the loop exited.

If you declatred I in the loop:

for (int i = 0 ; i < 5 ; i++)
{

}

Then i is undefined after the loop exit.

花心好男孩 2024-10-12 05:03:52

变量 i 是在循环范围之外定义的(这很好,否则在这种情况下您将无法打印它)。

并且它在循环的每一轮中都会进行后置调整,其结束条件是“当 i 大于或等于 5 时停止”。

所以此时 i 等于 5 确实非常有意义。

块作用域与 C 中的函数作用域并不完全相同。当您跳出循环作用域时,变量 i 不会神奇地“返回”到其先前的值。

Variable i is defined outside of the scope of the loop (which is great, or you wouldn't be able to print it in that case).

And it is post-icnremented for every-turn of the loop, for which the end condition is "stop when i is bigger or equal to 5".

So it really makes perfect sense for i to be equal to 5 at this point.

A block scope is not exactly the same as a function scope in C. The variable i doesn't "get back" magically to its previous value when you step out of the loop's scope.

回心转意 2024-10-12 05:03:52

循环后,i 的值将为 5。除非你

i = 50000;

在里面做了类似的事情。

i's value will be 5 after your loop. Unless you did something like

i = 50000;

inside of it.

━╋う一瞬間旳綻放 2024-10-12 05:03:52

在我读过的大多数编码标准中,通常建议不要在退出循环后使用“i”。特别不要这样做:

for(i = 0; i < num_elements; i++)
{
    if(element[i].id == id)
    {
        /* Do something to element here. */
        break;
    }
}

if(i == num_elements)
{
    fprintf(stderr, "Failed to find element %d.", id);
    succeeded == false;
}

虽然这可以工作,但编码很差。与替代方案相比,它的可读性和可维护性较差。例如

succeeded = false;

for(i = 0; i < num_elements; i++)
{
    if(element[i].id == id)
    {
        /* Do something to element here. */
        succeeded = true;
        break;
    }
}

if(false == succeeded)
{
    fprintf(stderr, "Failed to find element %d.", id);
}

It's also generally recommended against using "i" after you exit the loop in most coding standards I have ever read. In particular do NOT do:

for(i = 0; i < num_elements; i++)
{
    if(element[i].id == id)
    {
        /* Do something to element here. */
        break;
    }
}

if(i == num_elements)
{
    fprintf(stderr, "Failed to find element %d.", id);
    succeeded == false;
}

While this will work it is poor coding. It is less readable and maintainable than the alternatives. E.g.

succeeded = false;

for(i = 0; i < num_elements; i++)
{
    if(element[i].id == id)
    {
        /* Do something to element here. */
        succeeded = true;
        break;
    }
}

if(false == succeeded)
{
    fprintf(stderr, "Failed to find element %d.", id);
}
清晨说晚安 2024-10-12 05:03:52

是的,变量仅在声明它们的块内有效。
这是一个例子:

#include <stdio.h>

void main(int argc, char *argv[])
{
    if(argc == 2) {
        int x;
        x = 7;
    }

    x = 1;
}

这是编译器:

gcc ex.c
ex.c: In function ‘main’:
ex.c:10: error: ‘x’ undeclared (first use in this function)
ex.c:10: error: (Each undeclared identifier is reported only once
ex.c:10: error: for each function it appears in.)

Yes, variables are valid only inside the block in which they are declared.
Here's an example:

#include <stdio.h>

void main(int argc, char *argv[])
{
    if(argc == 2) {
        int x;
        x = 7;
    }

    x = 1;
}

That's the compiler:

gcc ex.c
ex.c: In function ‘main’:
ex.c:10: error: ‘x’ undeclared (first use in this function)
ex.c:10: error: (Each undeclared identifier is reported only once
ex.c:10: error: for each function it appears in.)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文