The C Programming Language, 2nd Edition, by Kernighan and Ritchie
Exercise 5.01 on page 97
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.
Solution by Gregory Pietsch
Here is Greg's solution:
#include <ctype.h> #include <stdio.h> /* for EOF */ int getch(void); void ungetch(int); /* getint: get next integer from input into *pn * 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. */ int getint(int *pn) { int c, sign, sawsign; while (isspace(c = getch())) /* skip white space */ ; if (!isdigit(c) && c != EOF && c != '+' && c != '-') { ungetch(c); /* it's not a number */ return 0; } sign = (c == '-') ? -1 : 1; if (sawsign = (c == '+' || c == '-')) c = getch(); if (!isdigit(c)) { ungetch(c); if (sawsign) 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; }










