scanf() misbehaving

前端 未结 3 405
野性不改
野性不改 2020-12-22 06:07

I have a very short snippet that reads in an integer:

#include 

int main() {
    while (1) {
        int i = 0;
        int r = scanf(\"%d\",         


        
3条回答
  •  攒了一身酷
    2020-12-22 06:27

    Man page says

    RETURN VALUES These functions return the number of input items assigned. This can be fewer than provided for, or even zero, in the event of a matching failure. Zero indicates that, although there was input available, no conversions were assigned; typ- ically this is due to an invalid input character, such as an alphabetic character for a `%d' conversion.

    Since you have used "%d" , if you use any number then this will work fine otherwise it will not consume it from the buffer

提交回复
热议问题