一个猜数程序中的scanf中空格问题
这是个猜数问题,猜完一个数后程序会问你是否继续猜下一个程序代码:
#include <stdio.h> #include <time.h> #include <stdlib.h> int main() { int magic; int guess; int count = 0; char reply; srand(time(NULL)); do { magic = rand()%100 +1; do { printf("guess a number: \n"); scanf("%d",&guess); count++; if( magic > guess) printf("too low \n"); else if( magic < guess) printf("too high \n"); } while((guess != magic) && (count < 10)); if(count < 10) printf("right"); else printf("no chance"); printf("do you want to contiune(y or n):"); scanf("%c",&reply); //问题就在这个scanf这里,答案中写着%c前面有一个空格,然后发现加了空格程序正常 }while (reply == 'y'); //没加空格后会跳过reply的输入,直接结束程序了 //非常困惑,,想问一下这里的%c前面空格到底有什么作用 printf("game over"); return 0; }