Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #105287
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: "usual arithmetic conversions": please explain something... |
| Date | 2017-03-01 09:43 -0800 |
| Organization | None to speak of |
| Message-ID | <ln8toodhad.fsf@kst-u.example.com> (permalink) |
| References | (1 earlier) <o951vq$ch6$2@dont-email.me> <ln1suhersb.fsf@kst-u.example.com> <o96d9b$p64$1@dont-email.me> <lnshmxc6bz.fsf@kst-u.example.com> <o96tr9$u0t$1@dont-email.me> |
BartC <bc@freeuk.com> writes:
> On 01/03/2017 16:25, Keith Thompson wrote:
>> BartC <bc@freeuk.com> writes:
>>> On 01/03/2017 00:59, Keith Thompson wrote:
>>>> BartC <bc@freeuk.com> writes:
>> [...]
>>>>> Try:
>>>>>
>>>>> printf("%u\n", c);
>>>>
>>>> No, c is an int.
>>>>
>>>> But do try:
>>>>
>>>> printf("%llu\n", a * b);
>>
>> My mistake, I meant "%lu\n", not "%llu\n".
>>
>>> It doesn't make much difference. Trying this (on a machine where long is
>>> 32 bits):
>>
>> Sure, if you use an incorrect format you can sometimes learn
>> something about the value being passed to printf, or its
>> representation -- but what's the point? c is of type int, and
>> happens to have a negative value; printing it as if it were an
>> unsigned int means you're lying to the compiler, and the behavior
>> is undefined. Printing the value of a * b shows you the actual
>> value that was converted to int.
>>
>> I showed how to print the actual values with their correct types.
>
> I showed how to convert the result of a signed/signed,
> unsigned/unsigned, signed/unsigned or unsigned/signed to a float, which
> is a better idea.
I showed how to print the actual values without converting them.
Converting to floating-point is not helpful, and introduces
irrelevant issues.
> Otherwise you're going always going to be forcing such a result into
> either a signed or unsigned int, so you will not know what it actually was.
No, the result is an unsigned long, which is what it actually is.
> In fact, the true result of the OP's calculation was 4294927296, with
> the operands being 4000 and 4294967286 (the -10).
Which calculation? There were several.
unsigned long a = 4000;
signed long b = -10;
int c;
c = a * b;
The result of the multiplication was a number of type unsigned long.
The result of the implicit conversion was a different number of type
int. The OP's original code combined those two operations; mine split
them up so you could see the intermediate result.
[...]
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
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