Signedness of chars

Mark Brader msb at sq.uucp
Mon Mar 7 09:54:19 AEST 1988


> > ...  That is, how many programs would break if
> > `unsigned char' and `char' were the same type?

Doug Gwyn notes:
> The most common portability error in this regard is
> 	char c;
> 	while ((c = getchar()) != EOF)
> 		...
[c should of course be int]

In fact, this is a bug in any case, if the code is supposed to handle binary
data.  On machines where chars are signed, this loop will wrongly terminate
as soon as it reads a byte containing the value (char)EOF, i.e. usually 0xFF.

I have encountered programs that failed in precisely this way.

Mark Brader			"When a supposedly indivisible transaction
SoftQuad Inc., Toronto		 fails to complete properly, this is known
utzoo!sq!msb, msb at sq.com	 as an atomic bomb."   -- Peter Neumann



More information about the Comp.lang.c mailing list