Path: utzoo!mnetor!uunet!mcvax!botter!ark!jcvw From: jcvw@cs.vu.nl (Winkel van Jan Christiaan) Newsgroups: comp.lang.c Subject: unsigned & char's Message-ID: <1167@ark.cs.vu.nl> Date: 7 Jan 88 15:35:32 GMT Organization: V.U. Informatica, Amsterdam, the Netherlands Lines: 36 Keywords: interpretation of type rules, signed char's, unsigned's I have the following question about typecasts and auto character conversion: Suppose I want to convert a character using a table, for example from ebcdic to ascii. I could do this in a way like this: extern char ebc2asc[]; /* this contains the ascii values for ebcdic characters. example: ebc2asc[0xf1]='1' */ char buf[100]; /* contains the characters to convert */ char *p /* a pointer */ p=buf; for (all characters in the buffer) { *p = ebc2asc[*p]; p++; } The problem is that characters like 0xf1 (ebcdic for '1') are negative when using signed char's (the default in most c's). The compiler will then generate code to sign extend my char to an int, causing a negative index into my array (i.e. 0xfff1 or 0xfffffff1).. Therefor I c use a typecast to an unsigned. The compiler will however still generate the same code. The code does not change until I define p to be an unsigned char *. I thought that the typecast made my wish clear to the compiler that I do not want s sign extension, but just an unsigned extension (with zeroes filled on the left of the old char) so that 0xf1 becomes 0x00f1 in stead of 0xfff1. It seems to be that there is an extra intermediate type: int. the path from char to unsigned is therefor not char --> unsigned but char --> int --> unsigned. I have tried to see what K&R say about it, but I could not really find it. Is there anybody out ther who knows how typecasts are defined in this context? Many thanks. Jan Christiaan van Winkel jcvw@cs.vu.nl typecast to a unsigned