getchar()在while循环中的行为

the*_*vvv 2 c getchar

我在终端上运行这个c程序

#include <stdio.h>

int main() {
    int result = 0;

    while(result <= 0) 
    {
        int result = (getchar() != EOF);
        result = 2;
        printf("x");        
    }

    printf("out\n");
}
Run Code Online (Sandbox Code Playgroud)

之后我输入单词"hello",然后返回.结果是我得到了多个'x'字符.

为什么不在第一个'x'之后终止?

ODY*_*Kon 5

你在resultwhile循环中重新声明(阴影).该result所中使用while(result <= 0)是在循环外部声明的一个.