Path: utzoo!utgpu!water!watmath!clyde!cbosgd!ihnp4!ihuxy!lied From: lied@ihuxy.ATT.COM (Bob Lied) Newsgroups: comp.lang.c Subject: Re: unsigned & char's Summary: typecast doesn't prevent standard conversion Keywords: interpretation of type rules, signed char's, unsigned's Message-ID: <2335@ihuxy.ATT.COM> Date: 11 Jan 88 00:22:27 GMT References: <1167@ark.cs.vu.nl> Organization: AT&T Bell Laboratories - Naperville, Illinois Lines: 30 In article <1167@ark.cs.vu.nl>, jcvw@cs.vu.nl (Winkel van Jan Christiaan) writes: > I thought that the typecast made my wish clear to the > compiler that I do not want sign extension, but just an unsigned extension > ... 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. Think of casting as an operator applied to the result of an expression, not an instruction to the compiler. In this case, the expression starts as type char, which by the "usual conversion" rules, is converted to an int -- a signed int with extension on your machine. After the equivalent int is computed, the cast operator is applied, but it's too late for you -- the high order bits have already changed to ones. By declaring the type as unsigned char, a different conversion rule applies, and you get an unsigned int even without a cast. The conversion rules are described in K&R appendix A, section 6.6, but it doesn't talk about unsigned *characters* explicitly. There is also a discussion of character conversions in section 5.1.3 and chapter 6 of Harbison & Steele[1]. The "usual conversions" are described (pretty clearly, I think) in section 6.3 of H&S. Bob Lied ihnp4!ihuxy!lied --------- [1] Harbison, S.P. and Steele, G.L. jr., "C: A Reference Manual," Prentice-Hall, Inc, 1987, ISBN 0-13-109810-1.