scanf ignoring, infinite loop

后端 未结 6 1018
遥遥无期
遥遥无期 2020-11-27 22:54
int flag = 0;
int price = 0;
while (flag==0)
{
    printf(\"\\nEnter Product price: \");
    scanf(\"%d\",&price);
    if (price==0) 
        printf(\"input not          


        
6条回答
  •  醉梦人生
    2020-11-27 23:26

    The "answers" that say it will because there is a '\n' in the buffer are mistaken -- scanf("%d", ...) skips white space, including newlines.

    It goes into an infinite loop if x contains 0 and scanf encounters a non-number (not just whitespace) or EOF because x will stay 0 and there's no way for it to become otherwise. This should be clear from just looking at your code and thinking about what it will do in that case.

提交回复
热议问题