Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #105231
| From | BartC <bc@freeuk.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: "usual arithmetic conversions": please explain something... |
| Date | 2017-03-01 12:04 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <o96d9b$p64$1@dont-email.me> (permalink) |
| References | <448topoppn.fsf@be-well.ilk.org> <o951vq$ch6$2@dont-email.me> <ln1suhersb.fsf@kst-u.example.com> |
On 01/03/2017 00:59, Keith Thompson wrote:
> BartC <bc@freeuk.com> writes:
>> On 28/02/2017 23:33, Lowell Gilbert wrote:
>>> Given the following program:
>>>
>>> #include <stdio.h>
>>>
>>> int main (void)
>>> {
>>> unsigned long a = 4000;
>>> signed long b = -10;
>>> int c;
>>>
>>> c = a * b;
>>> printf("%d\n", c);
>>>
>>> return 0;
>>> }
>>>
>>> I am surprised that I get a negative answer. From reading the standard,
>>> my expectation was that the multiplication would convert the signed
>>> operand to unsigned, based on this bit of the standard:
>>>
>>> Otherwise, if the operand that has unsigned integer type has rank
>>> greater or equal to the rank of the type of the other operand,
>>> then the operand with signed integer type is converted to the
>>> type of the operand with unsigned integer type.
>>>
>>> They've got the same rank, right?
>>>
>>> What am I missing? [It must be something, because I don't even get
>>> warnings on any of several compilers, with messages cranked up high.]
>>
>> Try:
>>
>> printf("%u\n", c);
>
> No, c is an int.
>
> But do try:
>
> printf("%llu\n", a * b);
It doesn't make much difference. Trying this (on a machine where long is
32 bits):
unsigned long a = 4000;
signed long b = -10;
int c;
c = a * b;
printf("%d\n", c);
printf("%u\n", c);
printf("%X\n", c);
printf("%d\n", a*b);
printf("%u\n", a*b);
printf("%X\n", a*b);
printf("%ld\n", a*b);
printf("%lu\n", a*b);
printf("%lX\n", a*b);
the output is:
-40000
4294927296
FFFF63C0
-40000
4294927296
FFFF63C0
-40000
4294927296
FFFF63C0
That suggests that it's the same pattern which is the expected -40000
when interpreted as a signed int, and the large positive value otherwise.
Changing a,b between both signed, both unsigned, unsigned/signed and
signed/unsigned, all produce the same results.
The cases where signed and/or unsigned make a difference to the actual
arithmetic performed can be tricky to find! I would experiment with
operations such as ">>" and ">".
Or using 'double c' (or a cast to double) instead of int c. That might
give more enlightening results, as then you are not imposing your own
signedness on the answer using an implicit conversion with c=a*b, or an
explicit one with "%lu":
double c = a*b;
printf("%f\n", c);
--
bartc
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
"usual arithmetic conversions": please explain something... Lowell Gilbert <lgusenet@be-well.ilk.org> - 2017-02-28 18:33 -0500
Re: "usual arithmetic conversions": please explain something... BartC <bc@freeuk.com> - 2017-02-28 23:45 +0000
Re: "usual arithmetic conversions": please explain something... Keith Thompson <kst-u@mib.org> - 2017-02-28 16:59 -0800
Re: "usual arithmetic conversions": please explain something... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-01 02:20 +0000
Re: "usual arithmetic conversions": please explain something... Keith Thompson <kst-u@mib.org> - 2017-02-28 18:51 -0800
Re: "usual arithmetic conversions": please explain something... BartC <bc@freeuk.com> - 2017-03-01 12:04 +0000
Re: "usual arithmetic conversions": please explain something... Keith Thompson <kst-u@mib.org> - 2017-03-01 08:25 -0800
Re: "usual arithmetic conversions": please explain something... BartC <bc@freeuk.com> - 2017-03-01 16:47 +0000
Re: "usual arithmetic conversions": please explain something... Keith Thompson <kst-u@mib.org> - 2017-03-01 09:43 -0800
Re: "usual arithmetic conversions": please explain something... BartC <bc@freeuk.com> - 2017-03-01 18:00 +0000
Re: "usual arithmetic conversions": please explain something... Keith Thompson <kst-u@mib.org> - 2017-03-01 10:15 -0800
Re: "usual arithmetic conversions": please explain something... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-01 19:08 +0000
Re: "usual arithmetic conversions": please explain something... supercat@casperkitty.com - 2017-03-01 09:01 -0800
Re: "usual arithmetic conversions": please explain something... jameskuyper@verizon.net - 2017-02-28 15:49 -0800
Re: "usual arithmetic conversions": please explain something... Keith Thompson <kst-u@mib.org> - 2017-02-28 16:03 -0800
Re: "usual arithmetic conversions": please explain something... Nick Bowler <nbowler@draconx.ca> - 2017-03-01 21:53 +0000
Re: "usual arithmetic conversions": please explain something... Keith Thompson <kst-u@mib.org> - 2017-03-01 14:18 -0800
Re: "usual arithmetic conversions": please explain something... Lowell Gilbert <lgusenet@be-well.ilk.org> - 2017-03-01 08:29 -0500
Re: "usual arithmetic conversions": please explain something... Lowell Gilbert <lgusenet@be-well.ilk.org> - 2017-03-01 09:11 -0500
Re: "usual arithmetic conversions": please explain something... Keith Thompson <kst-u@mib.org> - 2017-03-01 08:59 -0800
Re: "usual arithmetic conversions": please explain something... scott@slp53.sl.home (Scott Lurndal) - 2017-03-01 14:00 +0000
Re: "usual arithmetic conversions": please explain something... "James R. Kuyper" <jameskuyper@verizon.net> - 2017-03-01 10:58 -0500
csiph-web