CBFalconer said:
This is ridiculous. You are going on about this and have totally
removed the original code.
I didn't remove anything.
Your code was from another thread.
Charlie Gordon brought it up in this thread.
Anyhow, under the conditions that I stated previously
and which you snipped,
your loop will stop when getc(f) returns a value of 'A'.
You don't think that's a good way for the loop to operate,
do you?
Here's your code:
unsigned int rdvalue(FILE *f) {
unsigned int ch, value, err;
value = err = 0;
while (EOF != (ch = getc(f))) {
/* ch contains an int, which is the value of the char */
if (!isdigit(ch)) break; /* Only interested in digits */
ch = ch - '0'; /* form digit value */
if (((UINT_MAX - ch) / 10) > value) {
/* overflow detected, decide what to do */
ch = ch + '0'; /* restore char value */
break;
}
value = 10 * value + ch;
}
ungetc(ch, f); /* keep exit char for the user */
return err;
} /* untested - you check it out */
Here's the conditions under which your loop will stop
when getc(f) returns a value of 'A':
CHAR_BIT == 32
sizeof(int) == 2
INT_MAX == 0xffffffff
UINT_MAX == INT_MAX
EOF == ('A' - 1 - (unsigned char)-1)