Scanf不等待输入

时间:2018-04-15 16:56:05

标签: 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");
}

1 个答案:

答案 0 :(得分:1)

%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.