c - Scanf 不等待输入

标签 c

我有一个问题。当我运行它时,它只是垃圾邮件“更大”,而不是让我写入 scanf。有人可以尝试解释一下原因以及我可以采取哪些措施来解决它吗?

#include <stdio.h>
#include <time.h>

int main(){
    int rn, n=0;
    rn=rand();
    while (n!=rn){
        scanf("%n", &n);
        if (n<rn)
            printf("\nBigger\n");
        else if (n>rn)
            printf("\nSmaller\n");
    }
    printf("\nCorrect\n");
}

最佳答案

%n 不正确。您可能需要 %d 并且应该检查 scanf() 返回的内容以检查是否有错误。

来自man scanf:

 n     Nothing is expected; instead, the number of characters consumed
       thus far from the input is stored through the next pointer, which
       must be a pointer to int.  This is not a conversion, although it
       can be suppressed with the * flag.

关于c - Scanf 不等待输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49844506/

相关文章:

c - c中有符号和无符号字符之间的区别

c - 关于 OpenMP 和 -Ofast 的组合

c - 在gtk c中的对话框中打印输入框的值

c - 在 c 中清除输入缓冲区的替代方法

c - 从字符串到二进制文件

c++ - 如何创建一个将其证书发送到服务器的 Gnutls-Client?

c - 将管道设置为播放状态后,Gstreamer 回调未命中

c - 需要在 Yahtzee 中重新启动计算机

c - 返回不同数据类型的 C 函数的正确返回类型是什么

c++ - 从一个文件中读取多个 jpg 图像