/* Getop: get next operator or numeric operand. */
int getop (char s[]) {
int i, c;
while ((s[0] = c = getch()) == ' ' || c == '\t')
;
s[1] = '\0';
if (!isdigit(c) && c!= '.' && c!= '-')
return c; /* Not a number */
i = 0;
if (c == '-')
{
while (isdigit(s[++i] = c = getch()))
;
}
if (isdigit(c)) /* collect integer part */
while (isdigit(s[++i] = c = getch()))
;
if (c == '.')
while (isdigit(s[++i] = c = getch()))
;
s[i] = '\0';
if (c != EOF)
ungetch(c);
return NUMBER;
}
#define BUFSIZE 100
char buf[BUFSIZE];
int bufp = 0; /* next free position in buf */
int getch(void) /* get a (possibly pushed back) character */
{
return (bufp > 0) ? buf[--bufp] : getchar();
}
void ungetch(int c) /* push character back on input */
{
if (bufp >= BUFSIZE)
printf("ungetch: too many characters\n");
else
buf[bufp++] = c;
}
How should I do that? I know that static variables maintain their value at each invocation.
I'm not clear on what you mean. But getc/ungetc in the C standard library do exactly that. There's a static char and a flag somewhere that they use to "put back" a char. They only manage one char, not an infinite amount as your code does as that's all they needed in their parsers.
Anyway, if you explain a bit more about what you want we can be more helpful.