would you please clarify by an example?
calling ungetch places the character into the buffer buf and increases bufp if enough space is available.
The next time you call getch you want to read the pushed backed characters. Hence you check whethre some exist and if so return them, otherwise call getchar
ungetch('a') => buf = { 'a', ...... }
ungetch('b') => buf = { 'a', 'b', .... }
getch() => 'b' | buf = { 'a', .... }
getch() => 'a' | buf = { .... }
getch() => getchar()