How to write an 8-bit clean program

Jan B. Andersen jba at harald.ruc.dk
Fri Feb 16 02:34:08 AEST 1990


ra at is.uu.no (Robert Andersson) writes:

>I would like to discuss. What I would like is to hear other peoples opinion 
>on the signed/unsigned char issue.

>The type 'char' as defined in C on some implementations range from 
>-127 to 128, on others from 0 to 255. The rest of this message assumes
>the compiler has signed chars as a default.

>In an 8-bit set some character will have values > 128.

Formally speaking, no! As you point out yourself, we're talking about
8 bit character sets. This simply means, that all 8 bits are (potentially)
used to represent a character. Of course, one may look upon those bits
as an unsigned integer with values from 0 to 255.

>Is it kosher to store these values in char variables?

I should think so. As long as |char| is guaranteed to hold at least 8 bit,
the compiler should be able to map any character constant into a unique
value before storing it.

>Suppose you do it, then as long as you simply use char variables in
>simple assignments/test or as buffers, all is probably OK. As soon as
>you use the variable in arithmetic expressions or assigments to other
>types things become more muddy.

>So, the better way to do it might be to change all char variables in
>your program to unsigned char? But that opens another can of worms.
>Many compilers spit out warnings for expression like:
>	unsigned char *junk = "morejunk";

I'll take your word for it, although I don't see way it should complain.

>lint dislikes things like:
>	unsigned char buff[100];
>	write(fd, buff, 100);

Probably because |buff| was declared as |char *buff| in the header. It
might be more correct to declare it as |void *buff|.

>Opinions?

Just my 0.25 kr.
--
Jan B. Andersen <jba at dat.ruc.dk>             ("SIMULA does it with CLASS")



More information about the Comp.lang.c mailing list