Why unsigned chars not default?

Joe Herman dzoey at umd5.umd.edu
Tue Oct 25 10:21:30 AEST 1988


>From article <7354 at ihlpl.ATT.COM>, by knudsen at ihlpl.ATT.COM (Knudsen):
> In article <9563 at pur-ee.UUCP>, mendozag at 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(!).
Try this:
	union intaschar {
	    char hilo[sizeof (int)];
	    int val;
        } foo;

	foo.val = somenumber

	write (fh, foo.hilo, sizeof (int));

if for some reason he can't just write the integer out like below.

> 
> At least on a Motorola micro (6809 or 680x0) you can say
> write(chan, int, 2) and put out the whole integer at once.

Ick, I assume you mean:
  write (chan, &int, sizeof (int));   /* excuse the overloading of 'int' */

otherwise you're writing out two bytes of the address of 'int'.

Also, for PC's (at least with microsoft) make sure you open the file in
binary mode if you're going to do binary I/O.


> 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.

Remember, functions like getchar, getc &c, return an int, not a char
which gets you around the problem of '\0377' being confused with EOF.


> -- 
> 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."

Nice quote.

			Joe Herman
			The University of Maryland

dzoey at terminus.umd.edu


-- 
"Everything is wonderful until you know something about it."



More information about the Comp.lang.c mailing list