Skip to content

Latest commit

 

History

History
34 lines (25 loc) · 893 Bytes

File metadata and controls

34 lines (25 loc) · 893 Bytes

Exercise 4-8

Suppose that there will never be more than one character of pushback. Modify getch and ungetch accordingly.

Process

Just removed the array and updated the functions.

Not sure needed to go as far as zer0325 in renaming the variables. I wanted it to be a similar to the original as possible.

Note: I discovered you can add , between the ternary ?: statement. It also returns the most right expression in a chain of , expressions.

Code

char buf;       /* buffer for ungetch */
int bufp = 0;   /* next free position in buf */

int getch(void) /* get a (possibly pushed back) character */
{
    return (bufp == 1) ? bufp = 0, buf : getchar();
}

void ungetch(int c) /* push character back on input */
{
    buf = c, bufp = 1;
}

Output

No Output

Back to Main