3 ms·
> This is about a language construct (&& and ||) that I would argue is terrible and should never be used. Something tells me you'd hate my five-line implementa
by trealira 2y ago
> This is about a language construct (&& and ||) that I would argue is terrible and should never be used.
Something tells me you'd hate my five-line implementation of the standard library function fgets in C.
char *fgets(char *buf, size_t n, FILE *fp) {
char *p = buf;
int c = 0;
while (n > 1 && (c = getc(fp)) != EOF && (*p++ = c) != '\n')
n--;
return n > 0 && (c != EOF || feof(fp) && p != buf) ? *p = '\0', buf : 0;
}
I'm joking; I'd never write code this way in earnest.