# Thread: getop - inverse polish calculator - K&R

1. ## getop - inverse polish calculator - K&R

I will be grateful if someone could help me to understand this function.

Code:
```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;
}```
Thanks!

2. The first while loop skips whitespace (space characters or tabs). The other while loops do what they say on the tin (or in this case, the comments attached to them). The last part is ungetch(), which puts the last read character back in the stream to be read next time (since we read until we get something that doesn't fit, we have to put that thing that doesn't fit back into the input).

The only syntax trick involved here is that you can string multiple assignments together, so in something like
Code:
`s[0] = c = getch()`
the getch function is called, it gets a character, and that is assigned to c, and is then also assigned to s[0]. And the further reads also add those characters to s as well, so at the end of the day s contains the read-in material.

3. Thank you! It is quite clear.

4. s[1] = '\0';

but why we have this afterwards? I cannot get it

5. Strings must end in a \0 character.

6. ok tabstop... maybe this is too much! but please try to help...

let us say that I have input 1 2 +

could you explain how this will look like at the getop function!

7. Step through it and see. Remember that getch reads one character at a time, and ungetch puts one character back into the input stream.

8. tabstop have you ever read K&R? I am quite near to understand the inverse polish calculator and I solved "based on other solutions" most of the exercises. But now I am at page 80 -> 4.4 Scope of rules ... doest this means that I am not a simple beginner (anymore) and I will be able to progress even more if I finish with pointers and structures successfully (based on your and other helps) here at the forum? What is your opinion...

9. I have never read K&R, no. I would suppose that you should consider yourself "ready to move on" if you can work the exercises without 'based on other solutions'.