C getchar() 的误解

C getchar() misunderstanding

谁能给我解释一下代码?? d 不会总是等于 c 吗?我想我真的不明白这个 getchar() function.Why 是不是 d 总是等于 `c ?

 #include<stdio.h>

    void test(int c);

    int main(void) {
        int c;
        while ((c = getchar()) != EOF) {
            test(c);
        }
        return 0;
    }

    void test(int c) {
        int d;
        if (c == '/') {
            d = getchar();
            printf("%c", d);
        }
    }

输入:

/*

输出:

*

不,不是真的。正如 C11 章节 §7.21.7.6 中所述,getchar 函数,(强调我的

The getchar function returns the next character from the input stream pointed to by stdin. [...]

因此,每次调用 getchar() 都会为您提供输入流中存在的 下一个 字符输入。所以,当满足c == '/'条件时,它会读取下一个条目并存储到d,它不必与c相同。