我不明白为什么这正是我想要的。我在循环中使用两个scanf的部分使我感到困惑。我用devcpp编译了它。

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int dend, dsor, q, r;
    char c;
    while(c!='n')
    {
        printf("enter dividend: ");
        scanf("%d", &dend);
        printf("enter divisor: ");
        scanf("%d", &dsor);
        q=dend/dsor;
        r=dend%dsor;
        printf("quotient is %d\n", q);
        printf("remainder is %d\n", r);
        scanf("%c", &c);
        printf("continue? (y/n)\n");
        scanf("%c", &c);
    }
    system("PAUSE");
    return 0;
}

最佳答案

FWIW,您的代码调用undefined behavior。在部分

char c;
while(c!='n')


c是未初始化的局部变量,具有自动存储功能,您正在尝试使用不确定的c值。

就是说,首先scanf("%c", &c);用来吃掉输入缓冲区中存在的换行符,这是由于在先前输入之后按下了回车键。 You can read about it in details in another post

关于c - 为什么scanf在while循环中起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40180478/

10-11 22:39
查看更多