I would say that all 3 have drawbacks, but I favor the 3rd with a modification.
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.
YourThe third example does have a potential buglooks fine. In
(edit history: I made a mistake saying that ANSI or K&R C, there is no guaranteed-C did not guarantee left-to-right orderevaluation of execution, so checking that c != EOF does not necessarily occur after c = getchar(). The optimizer could do strange things with it&& and proposed a change. (In C90However, you can rely on && to force theANSI-C does guarantee left side to be evaluated before the right side)-to-right evaluation of &&.
I'm not sure about K&R C, but I would rewritecan't find any reference to it asand no one uses it anyways...)
//3rd approach int c; while((c = getchar()) != EOF) if (c == '\n') break;