# Thread: Question in getint #2

1. ## Question in getint #2

I can't understand what it's doing...

Code:
```
#include <ctype.h>
#include <stdio.h>

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

/* getint: get next integer from input into *pn */
int getint(int *pn)
{
int c, sign;

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 (c == '+' || c == '-')
c = getch();
for (*pn = 0; isdigit(c); c = getch())
*pn = 10 * *pn + (c - '0');   // This is what I don't get...
*pn *= sign;

if (c != EOF)
ungetch(c);
return c;
}```

2. Imagine the user enters:

12345

That's 5 ascii characters. If you subtract the ascii value of '0' from an ascii digit yeilds it's numerical value, ie: '8' - '0' == 8.

So with the equation:

value = (value * 10) + digit

...the number builds like this:

1 [ (0 * 10)+1 == 1 ]
12 [ (1 * 10)+2 == 12 ]
123 [ (12 * 10)+3 == 123 ]
1234 [ (123 * 10)+4 == 1234 ]
12345 [ (1234 * 10)+5 == 12345 ]

3. Originally posted by Sebastiani
Imagine the user enters:

12345

That's 5 ascii characters. If you subtract the ascii value of '0' from an ascii digit yeilds it's numerical value, ie: '8' - '0' == 8.

So with the equation:

value = (value * 10) + digit

...the number builds like this:

1 [ (0 * 10)+1 == 1 ]
12 [ (1 * 10)+2 == 12 ]
123 [ (12 * 10)+3 == 123 ]
1234 [ (123 * 10)+4 == 1234 ]
12345 [ (1234 * 10)+5 == 12345 ]
Now it makes sense.. I had no idea about '8' - '0' == 8
Thank you very very much.

4. ASCII '8' - '0' == 0x38 (decimal 56)

Unless of course we're talking about EBCDIC which
is apparently the case.

http://www.natural-innovations.com/c...ciiebcdic.html

5. ASCII '8' - '0' == 0x38 (decimal 56)

Unless of course we're talking about EBCDIC which
is apparently the case.
Eh? '8' - '0' == 8 not 0x38. Even on the EBCDIC system.

Only time this would break down is when the characters '0' to '9' aren't continuous or sequential

6. Yes, of course.

In my defense, parse error, hallucinating, and just woke up.

What I did learn though, was that EBCDIC is the reason that
the C programming language has trigraphs.