与自动和静态变量相关的混淆。

时间:2021-01-19 13:38:16
#include<stdio.h>
int main(){
    int a=10;
    {    printf("%d",a);
         int a=20;
         printf("%d",a);
    }
    printf(" %d",a);
    return 0;
}

Output:10 20 10

In the above code I understand that the visibility of variable a(inside inner block) has scope only within that block therefore I get that particular output. But the variable a which is declared outside that block should have its scope even within the inner block...Therefore how is it possible for me to again type int a=20; Shouldn't it give me an error like "redefinition of a" and "previous declaration of a was here". Like if I use

在上面的代码中,我理解变量a(内部内部块)的可见性只有在那个块内,因此我得到了特定的输出。但是,在该块之外声明的变量a应该有它的范围,甚至在内部块中…因此,我怎么可能再次输入int a=20;它不应该给我一个错误,比如“重新定义a”和“之前的a声明”。如果我使用

int b=10;
int b=15;

My second problem is this

我的第二个问题是。

void main() {
 static int a=10;

    {
         printf("%d ",a);
         static int a=20;
         printf("%d",a);
    }
    printf(" %d",a);

}

Apart from the same doubt as the previous code about why I'm not getting an error like "redefinition of a", This is my doubt related to this code.

除了前面的代码中关于为什么我没有“重新定义a”这样的错误之外,这是我对这段代码的怀疑。

For the above code i get the same Output: 10 20 10 but what I was expecting was

对于以上代码,我得到了相同的输出:10 20 10,但我所期望的是。

10 20 20 

I mean in the inner block once static int a is reinitialized to 20 shouldn't it be the same value even after it exits the block? because the a static variable's scope is throughout the entire program.

我的意思是,在内部块中,一旦静态int a被重新初始化为20,即使它退出了block,它也不应该是相同的值吗?因为静态变量的作用域贯穿整个程序。

2 个解决方案

#1


2  

Answer for the first problem: It is called variable shadowing. From Wikipedia:

第一个问题的答案:它被称为变量阴影。从*:

variable shadowing occurs when a variable declared within a certain scope (decision block, method, or inner class) has the same name as a variable declared in an outer scope.

当在某个范围内声明的变量(决策块、方法或内部类)与在外部范围声明的变量同名时,就会出现变量阴影。

Simply put, when you create a variable with the same name but in other scope it shadows the previous variable.

简单地说,当您创建一个名称相同但在其他范围内的变量时,它会使前面的变量黯然失色。

About the second problem - here is a fine example:

关于第二个问题,这里有一个很好的例子:

// static1.cpp
// compile with: /EHsc
#include <iostream>

using namespace std;
void showstat( int curr ) {
   static int nStatic;    // Value of nStatic is retained
                          // between each function call
   nStatic += curr;
   cout << "nStatic is " << nStatic << endl;
}

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

Output:

输出:

nStatic is 0

nStatic是0

nStatic is 1

nStatic是1

nStatic is 3

nStatic是3

nStatic is 6

nStatic是6

nStatic is 10

nStatic是10

#2


1  

#include<stdio.h>
int main(){
    int a=10;
    {    printf("%d",a);
         int a=20;
         printf("%d",a);
    }
    printf(" %d",a);
    return 0;
}

The second a is in a different scope, as you seem to already understand. The "redefinition of a" error only applies when you define two variables with the same name in the same scope.

第二个a在一个不同的范围内,你似乎已经理解了。“重新定义”错误只适用于在同一范围内定义两个具有相同名称的变量。

void main() {
    static int a=10;

    {
         printf("%d ",a);
         static int a=20;
         printf("%d",a);
    }
    printf(" %d",a);

}

Again, the a inside the inner block only has scope within that block. If you re-enter the same block, a will continue to have the same value because you declared it as static. However, outside of the block, you will use the value of first variable a. This illustrates the difference between a variable's scope and its lifetime.

同样,内块内的a只有该块内的范围。如果重新输入相同的块,则a将继续具有相同的值,因为您将其声明为静态的。但是,在块之外,您将使用第一个变量a的值。这说明了变量的范围和它的生存期之间的区别。

#1


2  

Answer for the first problem: It is called variable shadowing. From Wikipedia:

第一个问题的答案:它被称为变量阴影。从*:

variable shadowing occurs when a variable declared within a certain scope (decision block, method, or inner class) has the same name as a variable declared in an outer scope.

当在某个范围内声明的变量(决策块、方法或内部类)与在外部范围声明的变量同名时,就会出现变量阴影。

Simply put, when you create a variable with the same name but in other scope it shadows the previous variable.

简单地说,当您创建一个名称相同但在其他范围内的变量时,它会使前面的变量黯然失色。

About the second problem - here is a fine example:

关于第二个问题,这里有一个很好的例子:

// static1.cpp
// compile with: /EHsc
#include <iostream>

using namespace std;
void showstat( int curr ) {
   static int nStatic;    // Value of nStatic is retained
                          // between each function call
   nStatic += curr;
   cout << "nStatic is " << nStatic << endl;
}

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

Output:

输出:

nStatic is 0

nStatic是0

nStatic is 1

nStatic是1

nStatic is 3

nStatic是3

nStatic is 6

nStatic是6

nStatic is 10

nStatic是10

#2


1  

#include<stdio.h>
int main(){
    int a=10;
    {    printf("%d",a);
         int a=20;
         printf("%d",a);
    }
    printf(" %d",a);
    return 0;
}

The second a is in a different scope, as you seem to already understand. The "redefinition of a" error only applies when you define two variables with the same name in the same scope.

第二个a在一个不同的范围内,你似乎已经理解了。“重新定义”错误只适用于在同一范围内定义两个具有相同名称的变量。

void main() {
    static int a=10;

    {
         printf("%d ",a);
         static int a=20;
         printf("%d",a);
    }
    printf(" %d",a);

}

Again, the a inside the inner block only has scope within that block. If you re-enter the same block, a will continue to have the same value because you declared it as static. However, outside of the block, you will use the value of first variable a. This illustrates the difference between a variable's scope and its lifetime.

同样,内块内的a只有该块内的范围。如果重新输入相同的块,则a将继续具有相同的值,因为您将其声明为静态的。但是,在块之外,您将使用第一个变量a的值。这说明了变量的范围和它的生存期之间的区别。