4

In Dennis Ritchie's "C programming Language" book, In getop func, he states that s[1]='\0' why does he end the array on index 1? What's the significance and need?

In later part he does uses other parts of the array..

int getch(void);
void ungetch(int);

/* getop: get next character or numeric operand */
int getop(char s[])
{
    int i, c;
    while ((s[0] = c = getch()) == ' ' || c == '\t')
        ;
    s[1] = '\0';

    if (!isdigit(c) && c != '.')
        return c; /* not a number */

    i = 0;
    if (isdigit(c)) /* collect integer part */
        while (isdigit(s[++i] = c = getch()))
            ;

    if (c == '.') /* collect fraction part */
        while (isdigit(s[++i] = c = getch()))
            ;
    s[i] = '\0';

    if (c != EOF)
        ungetch(c);

    return NUMBER;
}
Lundin
  • 195,001
  • 40
  • 254
  • 396
ThunderPunch
  • 483
  • 1
  • 4
  • 16
  • 2
    Please indent your code properly. – RedX May 12 '15 at 07:17
  • Thanks RedX its now indented – ThunderPunch May 12 '15 at 07:22
  • 1
    Lines like `while ((s[0] = c = getch()) == ' ' || c == '\t');` make me wanna weep. – Erich Kitzmueller May 12 '15 at 07:22
  • @ammoQ Had missed the brackets around the assignments in an earlier comment. – Peter - Reinstate Monica May 12 '15 at 07:32
  • It's not just the brackets. Lines like that contain, to put it in a way that even a friend of mine, who likes to watch superheroe movies as long as they are from marvel, because dc comics' heroes are too stereotypical, might understand late at night when we meet on fridays for some beers, too much information at once. – Erich Kitzmueller May 12 '15 at 07:40
  • 1
    @ammoQ Indeed. And then people still recommend K&R, I have absolutely no idea why. Look at this _horrible_ code, it is not written by the OP, it is written by K&R! Had any rookie programmer written code like this today, he would have been immediately fired. – Lundin May 12 '15 at 08:20
  • @Lundin: Exactly. Apparently, aesthetics were not K&Rs top priority anyway. – Erich Kitzmueller May 12 '15 at 08:32
  • 1
    @ammoQ Nor teaching anyone proper programming, nor proof-reading or reviewing their own work... – Lundin May 12 '15 at 08:40

3 Answers3

8

Because the function might return before the remaining input is read, and then s needs to be a complete (and terminated) string.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

s[] might be only a numeric operand like "+","-" etc.

In this case s[1] have to be '\0' to let s be a proper string.

Duy Đặng
  • 409
  • 1
  • 7
  • 15
0

The zero char will be overwritten later, unless the test if (!isdigit(c) && c != '.') is true so that the function returns early. Some coding guidelines would discourage returns from the middle of a function, btw.

Only when the function returns early is the s[1] = '\0' relevant. Therefore, one could have coded

int getop(char s[])
{
    int i, c;
    while ((s[0] = c = getch()) == ' ' || c == '\t')
        ;

    if (!isdigit(c) && c != '.')
    {   
         s[1] = '\0'; /* Do this only when we return here */
         return c; /* not a number */
    }
    i = 0;
    if (isdigit(c)) /* collect integer part */
    /* ... */

It is terse code. These guys knew their language and algorithms. But the snippet lacks error handling and thus depends on correct input (invalid input may let s be empty or let s overflow).

That is not untypical. Processing of valid data is often straightforward and short; handling all the contingencies makes the code convoluted and baroque.

Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62