Path: utzoo!utgpu!water!watmath!clyde!att!ihlpl!knudsen From: knudsen@ihlpl.ATT.COM (Knudsen) Newsgroups: comp.lang.c Subject: Re: Why unsigned chars not default? Message-ID: <7354@ihlpl.ATT.COM> Date: 24 Oct 88 18:35:19 GMT References: <9563@pur-ee.UUCP> Distribution: na Organization: AT&T Bell Laboratories - Naperville, Illinois Lines: 44 In article <9563@pur-ee.UUCP>, mendozag@pur-ee.UUCP (Grado) writes: > A guy around here is trying to port to several machines a program he > hacked away in a PC using Lattice C. For some obscure reason in his > original program he decided to use only low-level I/O. That forced > him to "split" integers and then save them as 2 bytes and then later > when the file is read back the integers are put together(!). At least on a Motorola micro (6809 or 680x0) you can say write(chan, int, 2) and put out the whole integer at once. > require him to declare as unsigned char the I/O buffer (which he > also uses for arithmetic operations) else the chars are negative > numbers when the their contents represents value > 127. (He does > a lot of arithmetic with characters representing integers). This is often a problem. If he doesn't want to declare the buffer unsigned (or his compiler, like mine, doesn't support unsigned char), he can replace c with (c & 255) whenever c is used as an int. > He claims the compilers are at fault and that all the compilers > should have 'unsigned char' as default for characters so you > can do all sorts of arithmetic with them. All compilers should have unsigned char, but why as default? Half the time you want short-range *signed* variables -128 to +127. And if no unsigned char type is supported, the (c & 255) fixes it relatively cheap; the reverse fix (unsigned to signed) is harder. Also the (c & 255) fix protects you against unknown compilers, by guaranteeing unsigned no matter what the default is. I DO wish compilers would tell you somehow what the default is; the 3B2 compilers seem to default to unsigned char, which breaks a lot of old EOF loops. Finally, your friend should minimize char->int conversions as much as possible; read the stuff in, transfer to an int variable, and work on that exclusively. Since he learned C on it, he should now learn some more C by thoroughly re-working the code for style and efficiency anyway. I can't stomach some of the stuff I wrote a few years back. -- Mike Knudsen Bell Labs(AT&T) att!ihlpl!knudsen "Lawyers are like handguns and nuclear bombs. Nobody likes them, but the other guy's got one, so I better get one too."