scanf函数在while循环中跳过值以扫描字符值

时间:2016-07-03 17:34:43

标签: c scanf

#include<stdio.h>

int main()
{
    int i,t;
    char c;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",&c);
        printf("%c",c);
    }
    return 0;
}

在while循环中为什么它会跳过一些值。但它适用于整数值

2 个答案:

答案 0 :(得分:2)

%s用于扫描字符串(不是char),更改为%c

答案 1 :(得分:1)

this可能是一个解决方案。使用:

scanf(" %c",&c);

而不是:

scanf("%c",&c);