turner@imagen.UUCP (D'arc Angel) (10/10/86)
~~~~~~~~~~~~~~~\ lineater, \~~~~~~~~~~~~~~~~~~~~~
here's a cute bug in megamax C:
int c;
while((c = getchar()) != EOF) {
.
.
.
}
works fine but:
unsigned char c;
while((c = getchar()) != EOF) {
.
.
.
}
loops infinately, the problem is that megamax generates the
following code:
AND #0xff,D0 ;D0 has the result of getchar
CMP #-1,D0 ;ooppsss should be CMP.B
ah well
--
----
These are days for the locust to eat
- Winston Churchill
Name: James M. Turner
Mail: Imagen Corp. 2650 San Tomas Expressway, P.O. Box 58101
Santa Clara, CA 95052-8101
AT&T: (408) 986-9400
UUCP: ...{decvax,ucbvax}!decwrl!imagen!turner
CompuServe: 76327,1575
GEnie : D-ARCANGELFISCHER-MICHAEL@YALE.ARPA (10/14/86)
From James M. Turner:
here's a cute bug in megamax C:
int c;
while((c = getchar()) != EOF) {
.
.
.
}
works fine but:
unsigned char c;
while((c = getchar()) != EOF) {
.
.
.
}
loops infinately, the problem is that megamax generates the
following code:
AND #0xff,D0 ;D0 has the result of getchar
CMP #-1,D0 ;ooppsss should be CMP.B
As well it should! EOF is -1. An "unsigned" number is always
positive and hence can never equal a negative number. The reason
c must be an int and not a char is that getchar() might return any
any of 256 valid bytes as well as the EOF indicator -- 257 values
in all, and they won't fit into 8 bits.
--Mike Fischer <fischer@yale.arpa>
-------hmm@exunido.UUCP (10/15/86)
That's not a bug, that's a feature... The type of an assignment expression is the type of the variable assigned to, in this case unsigned char. Now tell me how an unsigned char can EVER have the value -1 (EOF) ? Hans-Martin
braner@batcomputer.TN.CORNELL.EDU (braner) (10/15/86)
[] EOF is NOT a char, not even an unsigned one. Also, all operations (e.g. the "c==EOF") are done with int's, not chars. Therefore you should declare c an int, as dictated by K&R in the bible... BTW, using chars, signed or unsigned, wastes time: it adds EXT.W to convert it into an int... (or the AND #0xFF if unsigned). Use the char type only for arrays, to save RAM. - Moshe Braner