Hacker News new | past | comments | ask | show | jobs | submit login

I think the one liner was supposed to show how short and simple programming can be, but I think it ultimately has the opposite effect.

It's basically a showcase of terrible programming idioms that you should never use.






To be fair, that's not actually in the book. I said it was based on a function from that book, but below is the version they actually write in the book; I apologize for unintentionally misleading you. I wrote the above function myself (based on this one below) to showcase the type of style I'm talking about.

  /* getline: read a line into s, return length */
  int getline(char s[], int lim)
  {
      int c, i;
      for (i=O; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
          s[i] = c;
      if (c == '\n') {
          s[i] = c;
          ++i;
      }
      s[i] = '\0';
      return i;
  }
They specifically don't use pointer arithmetic or the post-increment operator, because at that point in the book, they haven't introduced those yet.

However, that function I wrote isn't far off from the real Unix V7 version of fgets [0], which is a similar function to getline:

  char *
  fgets(s, n, iop)
  char *s;
  register FILE *iop;
  {
      register c;
      register char *cs;
  
      cs = s;
      while (--n>0 && (c = getc(iop))>=0) {
          *cs++ = c;
          if (c=='\n')
              break;
      }
      if (c<0 && cs==s)
          return(NULL);
      *cs++ = '\0';
      return(s);
  }
Like I said, the creators and first users of C seemed to really like expressions with side-effects, pointer arithmetic, and while-loops with empty bodies (though fgets doesn't have that). It's interesting how Go is different in this regard, considering Ken Thompson was a co-creator of both languages.

[0]: https://minnie.tuhs.org/cgi-bin/utree.pl?file=V7/usr/src/lib...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: