forked from steven-schronk/C-Programming-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex_5-1.c
More file actions
90 lines (69 loc) · 1.46 KB
/
ex_5-1.c
File metadata and controls
90 lines (69 loc) · 1.46 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <ctype.h>
#define BUFSIZE 100
char buf[BUFSIZE];
int bufp = 0;
/*
NOTE: getchar and ungetch work in tandem:
This allows the user to get a character from standard in, have a look at it
and then determine to use it or place it back into a buffer for later use.
If this buffer has any chars inside, thise will be poped off first
before getting more input from stdin.
*/
int getch(void);
void ungetch(int);
/* reads from buffer if buffer contains chars or calls getchar otherwise */
int getch(void)
{
return (bufp > 0) ? buf[--bufp] : getchar();
}
/* places pushed-back characters into a char array shared buffer */
void ungetch(int c)
{
if(bufp >= BUFSIZE)
printf("ungetch: too many characters\n");
else
buf[bufp++] = c;
}
/* get next integer from input and put into *pn */
int getint(int *pn)
{
int c, sign, signed_num;
while(isspace(c = getch()))
;
if(!isdigit(c) && c != EOF && c != '+' && c != '-')
{
ungetch(c);
return 0;
}
sign = (c == '-') ? -1 : 1;
if((signed_num = (c == '+' || c == '-')))
c = getch();
if(!isdigit(c))
{
ungetch(c);
if(signed_num)
ungetch((sign == -1) ? '-' : '+');
return 0;
}
for(*pn = 0; isdigit(c); c = getch())
*pn = 10 * *pn + (c - '0');
*pn *= sign;
if(c != EOF)
ungetch(c);
return c;
}
int main()
{
int n[5];
int retval = '\0';
retval = getint(n);
printf("Retval: %c\n", retval);
int i;
for(i = 0; i < 5; i++)
{
printf("%d", n[i]);
}
printf("\n");
return 1;
}