Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 内部增量i++;输出未更改的结果_C - Fatal编程技术网

C 内部增量i++;输出未更改的结果

C 内部增量i++;输出未更改的结果,c,C,我在阅读《C初级读物Plus》一书时遇到了这样一段代码: int main() { int x = 30; printf("x in outer block: %d at %p\n", x, &x); { int x = 77; // new x , hides first x printf("x in inner block: %d at %p\n", x, &x); } printf("x in out

我在阅读《C初级读物Plus》一书时遇到了这样一段代码:

int main()
{
    int x = 30;

    printf("x in outer block: %d at %p\n", x, &x);
    {
        int x = 77; // new x , hides first x
        printf("x in inner block: %d at %p\n", x, &x);
    }
    printf("x in outer block: %d at %p\n", x, &x);
    while (x++ < 33) //original x
    {
        int x = 100;
        x++;
        printf("x in while loop: %d at %p\n", x, &x);
    }
    printf("x in outer block: %d at %p\n", x, &x);

    return 0;
}
这让我很困惑

x in while loop: 101 at 0x7ffee7243780
x in while loop: 101 at 0x7ffee7243780
x in while loop: 101 at 0x7ffee7243780
我怎样才能使它输出

x in while loop: 101 at 0x7ffee7243780
x in while loop: 102 at 0x7ffee7243780
x in while loop: 103 at 0x7ffee7243780

之所以会发生这种情况,是因为在
循环范围内,变量
x
每次执行都被重新定义为等于100,这会影响外部定义的
x
,因此您不是将该变量增加3倍,而是将内部变量增加3倍。在每次新的迭代中,新的x增加1,在输出中看到101。

每次进入while循环时,将内部x值重置为100,您可以添加一个外部变量,并在while循环中增加它,将其值添加到100

int main()
{
    int x = 30;
    int y = 0;
    printf("x in outer block: %d at \n", x);
    {
        int x = 77; /* new x , hides first x*/
        printf("x in inner block: %d at \n", x);
    }
    printf("x in outer block: %d at\n", x);

    while (x++ < 33) /*original x*/
    {
        int x = 100;/*value resets on every entry to 100*/
        x++;
        x +=y;
        ++y;/*value is incremented on every entry */

        printf("x in while loop: %d at\n", x);
    }
    printf("x in outer block: %d at \n", x);
    return 0;
}
intmain()
{
int x=30;
int y=0;
printf(“外部块中的x在\n处:%d”,x);
{
int x=77;/*新x,隐藏第一个x*/
printf(“内部块中的x在\n处%d”,x);
}
printf(“外部块中的x在\n处:%d”,x);
而(x++<33)/*原始x*/
{
int x=100;/*值在每个条目上重置为100*/
x++;
x+=y;
++y、 /*值在每个条目上递增*/
printf(“while循环中的x在\n处:%d”,x);
}
printf(“外部块中的x在\n处:%d”,x);
返回0;
}

这里有很多解释

让我直接回答你的问题“我怎样才能使它输出?”

while(x++<33)//原始x
{
//int x=100;
静态int x=100;
x++;
printf(“while循环中的x:%d位于%p\n”,x,&x);
}
我想说的是,在这个while循环中,阅读一些关于
静态
变量和一般的
存储类

的内容:

while (x++ < 33) //original x
{
    int x = 100; // this x hides the original x
    x++;
    printf("x in while loop: %d at %p\n", x, &x);
}

变量阴影被认为是有害的,我明白了。@sp2dannyMake x`在第二个
中,而
循环
静态
。我记得C Primer Plus是一本相当不错的书,但这里有一点让我有所保留:请注意,在打印之前,您必须将地址强制转换为
void*
,以避免未定义的行为(但这可能会在本书后面介绍)。例如,
printf(“外部块中的x在%p\n处:%d”,x,(void*)&x)。整个想法可能是让您看到打印的地址,并意识到您正在处理不同的对象。请格式化您的答案并添加更多文本。您不需要复制整个代码,而只需要复制片段
while (x++ < 33) //original x
{
    //int x = 100;
    static int x = 100;
    x++;
    printf("x in while loop: %d at %p\n", x, &x);
}
while (x++ < 33) //original x
{
    int x = 100; // this x hides the original x
    x++;
    printf("x in while loop: %d at %p\n", x, &x);
}
while (x++ < 33) //original x
{
    static int x = 100;
    x++;
    printf("x in while loop: %d at %p\n", x, &x);
}