gpt4 book ai didi

C getchar() 的误解

转载 作者:太空宇宙 更新时间:2023-11-04 01:22:09 26 4
gpt4 key购买 nike

谁能给我解释一下代码?? d 不会总是等于 c 吗?我想我并没有真正理解这个 getchar() 函数。为什么 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< 相同,无论如何。

关于C getchar() 的误解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39772882/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com