Exercise 5-1. As written, getint treats a + or - not followed by a digit as a valid representation of zero. Fix it to push such a character back on the input.
/* Exercise 5-1. As written, getint treats a + or - not followed by a digit as a valid representation of zero. Fix it to push such a character back on the input. */#include<stdio.h>#include<ctype.h>#define SIZE 5intgetint(int*pn);intmain(){intn,a,array[SIZE];for(n=0;n<SIZE&&(a=getint(&array[n]))!=EOF;n++)printf("array[%d] = %d, a = %d\n",n,array[n],a);return0;}intgetch(void);voidungetch(int);/* getint: get next integer from input into *pn */intgetint(int*pn){intc,sign;while(isspace(c=getch()))/* skip white space */;if(!isdigit(c)&&c!=EOF&&c!='+'&&c!='-'){ungetch(c);/* it is not a number */return0;}sign=(c=='-')?-1:1;if(c=='+'||c=='-'){c=getch();if(!isdigit(c)){ungetch(c);/* it is not a number */return0;}}for(*pn=0;isdigit(c);c=getch())*pn=10**pn+(c-'0');*pn*=sign;if(c!=EOF)ungetch(c);returnc;}#define BUFSIZE 100charbuf[BUFSIZE];/* buffer for ungetch */intbufp=0;/* next free position in buf */intgetch(void)/* get a (possibly pushed-back) character */{return(bufp>0)?buf[--bufp]:getchar();}voidungetch(intc)/* push character back on input */{if(bufp>=BUFSIZE)printf("ungetch: too many characters\n");elsebuf[bufp++]=c;}