Static variables

I need to modify getup so that it does`t need to use ungetch(); I should do so using a static variable:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

 
  /* 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.
Topic archived. No new replies allowed.