[comp.sys.ibm.pc] tc bug?

uucp@occrsh.UUCP (09/25/87)

This has no doubt been discussed before, and I wasn't paying attention :-)

unsigned int xyz = 0x8000 ;

main()
{
  printf("xyz=%d\n", xyz) ;
}

...gives me -32768. Really screws things up trying to read files in 32k
chunks.

David Drexler
  uucp:  ihnp4!occrsh!squid!david  +++  SOURCEry System BBS, FidoNet 1:19/0
  vox:   <405> 720-9630            +++  <405> 728-2463 (2400/1200/300)

iverson@cory.Berkeley.EDU (Tim Iverson) (09/26/87)

In article <236@occrsh.ATT.COM>  writes:
>This has no doubt been discussed before, and I wasn't paying attention :-)
>
>unsigned int xyz = 0x8000 ;
>
>main()
>{
>  printf("xyz=%d\n", xyz) ;
>}
>
>...gives me -32768. Really screws things up trying to read files in 32k
>chunks.

Well, I've never used tc (and never will), but this isn't a bug.  It is
perfectly K&R kosher - %d implies *signed* int.  If you want an unsigned
int, you should use %u.  Always check the manual before assuming a compiler
bug; even with something as buggy as tc, its more likely to be your fault.


- Tim Iverson
  iverson@cory.Berkeley.EDU
  ucbvax!cory!iverson

richardh@killer.UUCP (09/27/87)

In article <236@occrsh.ATT.COM>, uucp@occrsh.UUCP writes:
> unsigned int xyz = 0x8000 ;
> 
> main()
> {
>   printf("xyz=%d\n", xyz) ;
> }
> 
> ...gives me -32768. Really screws things up trying to read files in 32k
> chunks.

Printf only knows the types of its parameters by looking at the format string.
The conversion char %d specifies a signed int. Use %u (or %o or %x) if you
want to see the 16-bit value as an unsigned. 

The bit pattern 0x8000 IS -32768 when interpreted as a signed two's-complement
decimal number.

richard hargrove
..!inhp4!killer!richardh
------------------------ 

mac@idacrd.UUCP (Bob McGwier) (09/28/87)

in article <236@occrsh.ATT.COM>, uucp@occrsh.UUCP says:
> 
> This has no doubt been discussed before, and I wasn't paying attention :-)
> 
> unsigned int xyz = 0x8000 ;
> 
> main()
> {
>   printf("xyz=%d\n", xyz) ;
> }
> 
> ...gives me -32768. Really screws things up trying to read files in 32k
> chunks.

I think the problem is yours and not tc.  


change

printf("xyz=%u\n",xyz);

You are passing printf a number and telling it that it is a SIGNED
integer with the %d.

Bob