# getop - inverse polish calculator - K&R

This is a discussion on getop - inverse polish calculator - K&R within the C Programming forums, part of the General Programming Boards category; I will be grateful if someone could help me to understand this function. Code: int getch(void); void ungetch(int); /* getop: ...

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!