我按照C Primer Plus的说明编写了以下代码:
#include <stdio.h>
int main(void) {
int num;
int x = scanf("%d", &num);
printf("%d", x);
}
运行时:
$ ./a.out
t
0
$ ./a.out
2
1
scanf 如何检测“0”或“1”值?
最佳答案
它不会检测该值,而是返回它。它读取一个整数并将其放入 num
中。 scanf
的返回值不是它从用户输入中读取的值,而是读取的值的数量,在您的情况下最多可以为 1。
关于c - scanf ("%d", &num) 的状态为 0 或 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52816484/