Refinement in getop ()

In Dennis Ritchie's book The C Programming Language, In the getop function, he states that s [1] = '\ 0' why does he end the array at index 1? What is the meaning and necessity?

In a later part, it 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;
}
+4
source share
3 answers

Since the function can return before the remaining input is read, and then sshould be a complete (and complete) line.

+8
source

s [] can only be a numeric operand of type "+", "-", etc.

In this case, it s[1]must be '\0'that s is the correct string.

0
source

char , , (!isdigit(c) && c != '.'), , . , . .

, s[1] = '\0' .

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 */
    /* ... */

. . , , ( s s).

. ; .

0

All Articles