Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #105182 > unrolled thread

"usual arithmetic conversions": please explain something...

Started byLowell Gilbert <lgusenet@be-well.ilk.org>
First post2017-02-28 18:33 -0500
Last post2017-03-01 10:58 -0500
Articles 2 on this page of 22 — 9 participants

Back to article view | Back to comp.lang.c


Contents

  "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

Page 2 of 2 — ← Prev page 1 [2]


#105247

Fromscott@slp53.sl.home (Scott Lurndal)
Date2017-03-01 14:00 +0000
Message-ID<quAtA.20420$eM7.13656@fx14.iad>
In reply to#105182
Lowell Gilbert <lgusenet@be-well.ilk.org> writes:
>
>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:

 %d implicitly treats the value as a signed value, regardless of
the type associated with 'c'.

[toc] | [prev] | [next] | [standalone]


#105261

From"James R. Kuyper" <jameskuyper@verizon.net>
Date2017-03-01 10:58 -0500
Message-ID<81b95dcf-7184-0e01-62c3-5d156d1415bf@verizon.net>
In reply to#105247
On 03/01/2017 09:00 AM, Scott Lurndal wrote:
> Lowell Gilbert <lgusenet@be-well.ilk.org> writes:
>>
>> 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:
>
>  %d implicitly treats the value as a signed value, regardless of
> the type associated with 'c'.

More precisely, the correct type for "%d" is 'int', and the behavior is 
undefined if an argument has a type that is not the correct one for the 
corresponding conversion specifier (7.21.6.1p9). Treating an unsigned 
value as if it were signed is one of the most likely consequences of 
that fact, but there's literally no limits to what the standard actually 
permits such code to do.

Since c has the correct type for %d, that's not the reason for the 
unexpected behavior of this code. The problem is with the assignment 
statement, not the call to printf().

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.c


csiph-web