c在循环和外循环中的可变重新声明?

时间:2021-10-19 00:23:28

When int i; statement is declared 2 times in a program it shows errors but where as when int i; is written in a for loop that runs two times, it does not show any error.

当int i;语句在程序中声明2次显示错误,但在int i时;写入一个运行两次的for循环,它不会显示任何错误。

#include<stdio.h>//code 1 showing error
int main()
{
    int i;
    int i;
    return 0;
}
#include<stdio.h>//code 2 no error
int main()
{
    for(int j=1;j<=2;j++)
        int i;
    return 0;
}

3 个解决方案

#1


0  

In order to understand your problem, also called variable's scope, let's see to the following sample program:

为了理解您的问题,也称为变量的范围,让我们看看以下示例程序:

#include <stdio.h> 

int main(int argc, char *argv[])
{
    int I = -1;
    for (int I = 0; I < 3; I++) {
        printf("%d\n", I);
    }
    printf("%d\n", I);
    {
        int I = 200;
        printf("%d\n", I);
    }
    return 0;
}

As you can see I declared the variable I three times.

如你所见,我将变量I声明了三次。

When declared into the loop the result will be the Printing of the following values:

声明到循环中时,结果将是打印以下值:

0
1
2

After the for loop when I print again the I variable now I refer to the variable declared outside the for loop, the first one I declaration so the result will be:

在for循环之后,当我再次打印I变量时,我引用在for循环外声明的变量,我声明的第一个变量,结果将是:

-1

Now if I open a new scope with the curly braces and I declare a new variable with the same name but with a different value I will get:

现在,如果我用花括号打开一个新的范围,我声明一个具有相同名称但具有不同值的新变量,我会得到:

200

I hope my description about the variable's scope is now clear

我希望我对变量范围的描述现在很清楚

#2


1  

It is because the declared variable's scope is only inside the block of code where you have declared her. It is not seen outside that block.

这是因为声明的变量的范围仅在您声明她的代码块内。在那个街区之外没有看到它。

I have seen your code now.It's clear as day that in 1. code you declared two ints with same name which is not allowed and in 2. code you declared two int with different name so it's good.

我现在已经看到了你的代码。很明显,在1.代码中,你声明了两个具有相同名称的int,这是不允许的,并且在2.代码中你声明了两个具有不同名称的int,所以它很好。

#3


0  

The first main attempts to define two variables with equal identifiers. This is obviously forbidden and results in a compilation error.

第一个主要尝试使用相同的标识符定义两个变量。这显然是被禁止的,并导致编译错误。

The second main contains a for-loop. The for-loop introduces a new scope, which, if no curly braces are added, only embraces the next statement or definition (i.e., int i;).
i is allocated and deallocated for every iteration, so it really exists just once at a time. All is ever defined are in distinct scopes. Therefore there is no naming conflict.

第二个主要包含一个for循环。 for循环引入了一个新的作用域,如果没有添加花括号,它只包含下一个语句或定义(即int i;)。我为每次迭代分配和释放,因此它一次只存在一次。所有被定义的都在不同的范围内。因此没有命名冲突。


If you have a variable in outer scope (i.e., function scope of main) and one in an inner scope (i.e., scope of the for-loop), the variable from the inner scope hides the one from the outer scope, thus rendering the outer-scope variable inaccessible.

如果外部作用域中的变量(即main的函数作用域)和内部作用域中的变量(即for循环的作用域),则内部作用域中的变量会隐藏外部作用域中的变量,从而呈现外部范围变量不可访问。

The C11 standard says, §6.2.1.4:

C11标准说,§6.2.1.4:

Within the inner scope, the identifier designates the entity declared in the inner scope; the entity declared in the outer scope is hidden (and not visible) within the inner scope.

在内部范围内,标识符指定在内部范围内声明的实体;在外部作用域中声明的实体在内部作用域内是隐藏的(并且不可见)。

#1


0  

In order to understand your problem, also called variable's scope, let's see to the following sample program:

为了理解您的问题,也称为变量的范围,让我们看看以下示例程序:

#include <stdio.h> 

int main(int argc, char *argv[])
{
    int I = -1;
    for (int I = 0; I < 3; I++) {
        printf("%d\n", I);
    }
    printf("%d\n", I);
    {
        int I = 200;
        printf("%d\n", I);
    }
    return 0;
}

As you can see I declared the variable I three times.

如你所见,我将变量I声明了三次。

When declared into the loop the result will be the Printing of the following values:

声明到循环中时,结果将是打印以下值:

0
1
2

After the for loop when I print again the I variable now I refer to the variable declared outside the for loop, the first one I declaration so the result will be:

在for循环之后,当我再次打印I变量时,我引用在for循环外声明的变量,我声明的第一个变量,结果将是:

-1

Now if I open a new scope with the curly braces and I declare a new variable with the same name but with a different value I will get:

现在,如果我用花括号打开一个新的范围,我声明一个具有相同名称但具有不同值的新变量,我会得到:

200

I hope my description about the variable's scope is now clear

我希望我对变量范围的描述现在很清楚

#2


1  

It is because the declared variable's scope is only inside the block of code where you have declared her. It is not seen outside that block.

这是因为声明的变量的范围仅在您声明她的代码块内。在那个街区之外没有看到它。

I have seen your code now.It's clear as day that in 1. code you declared two ints with same name which is not allowed and in 2. code you declared two int with different name so it's good.

我现在已经看到了你的代码。很明显,在1.代码中,你声明了两个具有相同名称的int,这是不允许的,并且在2.代码中你声明了两个具有不同名称的int,所以它很好。

#3


0  

The first main attempts to define two variables with equal identifiers. This is obviously forbidden and results in a compilation error.

第一个主要尝试使用相同的标识符定义两个变量。这显然是被禁止的,并导致编译错误。

The second main contains a for-loop. The for-loop introduces a new scope, which, if no curly braces are added, only embraces the next statement or definition (i.e., int i;).
i is allocated and deallocated for every iteration, so it really exists just once at a time. All is ever defined are in distinct scopes. Therefore there is no naming conflict.

第二个主要包含一个for循环。 for循环引入了一个新的作用域,如果没有添加花括号,它只包含下一个语句或定义(即int i;)。我为每次迭代分配和释放,因此它一次只存在一次。所有被定义的都在不同的范围内。因此没有命名冲突。


If you have a variable in outer scope (i.e., function scope of main) and one in an inner scope (i.e., scope of the for-loop), the variable from the inner scope hides the one from the outer scope, thus rendering the outer-scope variable inaccessible.

如果外部作用域中的变量(即main的函数作用域)和内部作用域中的变量(即for循环的作用域),则内部作用域中的变量会隐藏外部作用域中的变量,从而呈现外部范围变量不可访问。

The C11 standard says, §6.2.1.4:

C11标准说,§6.2.1.4:

Within the inner scope, the identifier designates the entity declared in the inner scope; the entity declared in the outer scope is hidden (and not visible) within the inner scope.

在内部范围内,标识符指定在内部范围内声明的实体;在外部作用域中声明的实体在内部作用域内是隐藏的(并且不可见)。