K & R C Programs Exercise 5-1.

K and R C, Solution to Exercise 5-1:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C Program to get next integer from input into space, so that getint treats a + or – not followed by a digit as a valid representation of zero and fix it to push such a character back on the input. Read more about C Programming Language .

* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
* Happy Coding


int getch(void);
void ungetch(int);

/* getint: get next integer from input into *pn */
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)) {
if (sawsign)
ungetch((sign == -1) ? '-' : '+');
return 0;
for (*pn = 0; isdigit(c); c = getch())
*pn = 10 * *pn + (c - '0');
*pn *= sign;
if (c != EOF)
return c;
Read more Similar C Programs
C Basic
C Strings
K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

Leave a Reply