Article View: pe.cust.bugs
Article #17short casts, and long = {short,char} = long, don't work
From: Mark Brader
Date: Sat, 09 Mar 1985 18:43
Date: Sat, 09 Mar 1985 18:43
86 lines
2333 bytes
2333 bytes
We run Edition VII on a 3220.
This program:
#include <stdio.h>
main()
{
long l[5];
short s;
l[0] = 0x234567;
l[1] = s = l[0];
l[2] = s;
l[3] = (short) l[0];
l[4] = (short) 0x234567;
for (s = 0; s < 5; ++s)
printf ("l[%d] = %x\n", s, l[s]);
printf ("\n(short)0x234567 = %x\n",
(short)0x234567);
printf ("\nsizeof (short) = %d\nsizeof (long) = %d\n",
sizeof (short), sizeof (long));
}
should output 4567 for l[1] through l[4] and for the cast in the second-last
printf. In fact, the result is:
l[0] = 456789
l[1] = 456789
l[2] = 6789
l[3] = 456789
l[4] = 456789
(short)0x456789 = 456789
sizeof (short) = 2
sizeof (long) = 4
If char is substituted throughout for short, the casts work, but l[1]
(long = char = long) is still wrong. The output then is:
l[0] = 234567
l[1] = 234567
l[2] = 67
l[3] = 67
l[4] = 67
(char)0x234567 = 67
sizeof (char) = 1
sizeof (long) = 4
In case anyone doesn't believe that l[1] is wrong in both the above
examples, they should consider this program:
#include <stdio.h>
main()
{
long l1, l2;
float f;
l1 = f = 123456789;
l2 = f;
printf ("%d\n%d\n", l1, l2);
}
This correctly produces:
123456784
123456784
Notice the loss of precision from the type conversions. Of course, an
assignment is an expression whose type is that of the variable assigned to.
I discovered the bug while debugging my signature line below.
It doesn't work on this machine for type short!
{ allegra | decvax | duke | ihnp4 | linus | watmath | ... } !utzoo!lsuc!msb
also via { hplabs | amd | ... } !pesnta!lsuc!msb
Mark Brader and uw-beaver!utcsri!lsuc!msb
#define msb(type) (~(((unsigned type)-1)>>1))
Technical Details
Message-ID:
<492@lsuc.UUCP>Path:
arc1-ca.pugleaf.net!lsuc!msbShow Full Headers
Complete Article Headers
Relay-Version: version B 2.10.2 9/18/84; site lsuc.UUCP
Posting-Version: version B 2.10.2 9/18/84; site lsuc.UUCP
Path: arc1-ca.pugleaf.net!lsuc!msb
From: msb@lsuc.UUCP (Mark Brader)
Newsgroups: pe.cust.bugs
Subject: short casts, and long = {short,char} = long, don't work
Message-ID: <492@lsuc.UUCP>
Date: 9 Mar 85 18:43:39 GMT
Date-Received: 9 Mar 85 18:43:39 GMT
Reply-To: msb@lsuc.UUCP (Mark Brader)
Distribution: pe
Organization: Law Society of Upper Canada, Toronto
Lines: 86
Summary: The long value gets carried through.