-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy path4-8.c
49 lines (41 loc) · 790 Bytes
/
4-8.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* Exercise 4-8. Suppose that there will never be more than one character of
* pushback. Modify getch and ungetch accordingly.
*
* Faisal Saadatmand
*/
#include <stdio.h>
/* functions */
int getch(void);
void ungetch(int);
/* globals */
int buf; /* buffer from ungetch */
/* getch: get a (possibly pushed back) character */
int getch(void)
{
int c;
if (buf) {
c = buf;
} else
c = getchar();
buf = 0;
return c;
}
/* ungerch: push character back on input */
void ungetch(int c)
{
if (buf)
printf("ungetch: too many characters\n");
else
buf = c;
}
/* test getch */
int main(void)
{
ungetch('A');
ungetch('B'); /* should give error */
printf ("%c\n", getch());
ungetch('C');
printf ("%c\n", getch());
return 0;
}