为什么不声明具有相同的名称的变量之外,在一个循环内产生的错误? [英] Why doesn't declaring a variable with the same name outside and inside a loop produce an error?

查看:68
本文介绍了为什么不声明具有相同的名称的变量之外,在一个循环内产生的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  {
    INT I;
    对于(I = 0; I&小于5;我+ +)
    {
        INT I = 10;
        的printf(%D,我);
    }
}

我有两个问题。


  1. 为什么会出现在没有重新声明的错误,我

  2. 为什么输出将 10 5倍,而不是 10 1的时间?


解决方案

这一切都与范围的标识符要做。标识符是简单地给一个实体的名称(对象,函数,typedef名称等)在C和,按 C11 6.2.1 / 1


  

相同的标识符可以在程序的不同点表示不同的实体。


一个实体的范围在 / 2 那一段描述:


  

有关该标识符指定各不同的实体,所述标识符是可见的(即,可以使用)仅在程序文本的区域称为其范围


/ 4 涵盖您的具体情况:


  

如果一个标识符指定在同一名称空间两个不同的实体,该范围可能会重叠。如果是这样,一个实体(内部范围)的范围,对其他实体的范围(外部范围)前严格结束。


在换句话说,是这样的:

  {
    INT I = 42;
    的printf(%D,我);
    {
        INT I = 17;
        的printf(%D,我);
    }
    的printf(%d个\\ N,I);
}

是完全有效,并将打印 42 17 42 。这是因为该标识符 I 里面的内部块在其的的范围,这在第一右括号结束。

在您的特定情况下,可以把它看作是:

  {
    INT I; \\
    对于(I = 0; I&小于5;我+ +)GT;外我范围
    {/
        INT I = 10; \\
        的printf(%D,我); >我内心范围
    } /
}

INT I = 10 有效的隐藏外 I 为主体的持续时间循环。这就是为什么它打印大量的 10 真是让人不是 0..4

的范围I 结束在循环体的右括号,这样,当为循环的延续检查条件,它再次看到外 I 。这就是为什么它循环,而五次超过一次。

{  
    int i;  
    for(i=0;i<5;i++)  
    {  
        int i=10;  
        printf("%d",i);  
    }  
}  

I have two questions

  1. Why is there no redeclaration error for i?
  2. why output will be 10 5 times and not 10 1 time?

解决方案

It all has to do with the scope of an identifier. An identifier is simply a name given to an entity (object, function, typedef name and so on) in C and, as per C11 6.2.1 /1:

The same identifier can denote different entities at different points in the program.

The scope of an entity is described in /2 of that section:

For each different entity that an identifier designates, the identifier is visible (i.e., can be used) only within a region of program text called its scope.

And /4 covers your specific case:

If an identifier designates two different entities in the same name space, the scopes might overlap. If so, the scope of one entity (the inner scope) will end strictly before the scope of the other entity (the outer scope).

In other words, something like:

{
    int i = 42;
    printf ("%d ", i);
    {
        int i = 17;
        printf ("%d ", i);
    }
    printf ("%d\n", i);
}

is perfectly valid, and will print 42 17 42. That's because the identifier i inside the inner block is in its own scope, which ends at the first closing brace.

In your particular case, you can think of it as:

{
    int i;              \
    for(i=0;i<5;i++)     > outer i scope
    {                   /
        int i=10;       \
        printf("%d",i);  > inner i scope
    }                   /
}

The inner int i=10 effective hides the outer i for the duration of the body of the for loop. That's why it prints a lot of 10s rather than 0..4.

The scope of the inner i finishes at the closing brace of the for loop body so that, when the continuation condition of the for loop is checked, it once again sees the outer i. That's why it loops five times rather than once.

这篇关于为什么不声明具有相同的名称的变量之外,在一个循环内产生的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆