Hi, I am reading the book by kerninghan Ritchie Edition 2. It contains a code function on getch()
, ungetch()
, getop()
, etc. I am not able to understand the stuff and I feel that it is bit incomplete also. Therefore, I am posting the question here.
Not able to understand... what getch(), ungetch() functions are doing. Please give few examples to demonstrate these functions.
At the end. these getch() and ungetch(0 functions are used by getOp(). What does this function doing. Only examples willl do. No code explanations reqd.That i will try and manage myself. If i have some sample examples.
Please give few examples to make this code look pretty easier; if there is any correction in the programm, then please let me know.
Thanks
#include <stdio.h>
#include <ctype.h>
#define BUFSIZE 100
char buf[BUFSIZE]; /* buffer for ungetch */
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;
}
/* 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 fractional part */
while(isdigit(s[++i] = c = getch()));
s[i] = '\0';
if (c != EOF)
ungetch(c);
}