Difference between scanf() and strtol() / strtod() in parsing numbers

后端 未结 8 1597
陌清茗
陌清茗 2020-12-05 20:28

Note: I completely reworked the question to more properly reflect what I am setting the bounty for. Please excuse any inconsistencies with already-given ans

8条回答
  •  一生所求
    2020-12-05 20:54

    I am not sure how implementing scanf() may be related to ungetc(). scanf() can use up all bytes in the stream buffer. ungetc() simply pushes a byte to the end of buffer and the offset is also changed.

    scanf("%d", &x);
    ungetc('9', stdin);
    scanf("%d", &y);
    printf("%d, %d\n", x, y);
    

    If the input is "100", the output is "100, 9". I do not see how scanf() and ungetc() may interfere with each other. Sorry if I added a naive comment.

提交回复
热议问题