-1

In glibc reference we find such words:

...sloppy code like

{
  int c;
  ...
  while ((c = getc (fp)) < 0)
    ...
}

has to be rewritten...

Why testing for the sign of the int is called "sloppy code" in glibc reference?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Igor Liferenko
  • 1,499
  • 1
  • 13
  • 28
  • It's already explained just below (your quote) i.e. WEOF need not be nagative. – P.P Nov 21 '16 at 09:47
  • 1
    Just read text around that code more carefully. It is not about testing of sign. It is about comparing result with `WEOF / EOF` explicitly. `EOF` is required to be negative, and this is the only negative value that `getc()` may return. So it is a popular bad habit to write `getc() < 0` instead of `getc() != EOF`. And this habit may be interpolated to `wgetc()`, but since `WEOF` need not to be negative, code like `wgetc() < 0` may not work as expected. – Sergio Nov 21 '16 at 09:50
  • @Sergio: this remark about `WEOF` and `EOF` is strictly correct, but it would be intentionally misleading to defined `WEOF` do something different from `EOF` and that either of these be different from `-1`. – chqrlie Nov 21 '16 at 09:53
  • @chqrlie Yes, It surely would be misleading. Nevertheless MSVC headers declare `wint_t` as `unsigned short`, and `WEOF` like `(wint_t)0xFFFF`. That is definitely not a `-1`. – Sergio Nov 21 '16 at 11:09

1 Answers1

2

This is related to the use of WEOF macro. As already mentioned in the reference, (emphasis mine)

WEOF need not be the same value as EOF and unlike EOF it also need not be negative. [...]

So, checking for < 0 might be a wrong decision, strictly speaking, it should be checked against the return value of WEOF itself.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • @IgorLiferenko it is not strictly about `WEOF`, this is a generic problem, the example used there is `WEOF`, is that's your concer regarding the _sloppy_ part. – Sourav Ghosh Nov 22 '16 at 05:28