Flushing stdin after every input - which approach is not buggy?

前端 未结 5 520
执笔经年
执笔经年 2020-12-06 07:44

After Mark Lakata pointed out that the garbage isn\'t properly defined in my question I came up with this. I\'ll keep this updated to avoid confusions.


5条回答
  •  囚心锁ツ
    2020-12-06 08:08

    The first 2 examples use a feature of scanf that I didn't even know existed, and I'm sure a lot of other people didn't know. Being able to support a feature in the future is important. Even if it was a well known feature, it will be less efficient and harder to read the format string than your 3rd example.

    The third example looks fine.

    (edit history: I made a mistake saying that ANSI-C did not guarantee left-to-right evaluation of && and proposed a change. However, ANSI-C does guarantee left-to-right evaluation of &&. I'm not sure about K&R C, but I can't find any reference to it and no one uses it anyways...)

提交回复
热议问题