Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.c

Reply
 
Thread Tools Display Modes
  #16 (permalink)  
Old 05-31-2012, 05:22 PM
James Kuyper
Guest
 
Posts: n/a
Default Re: Why, warning: comparison between signed and unsigned integerexpressions?

On 05/31/2012 01:09 PM, Joe Pfeiffer wrote:
> Ike Naar <ike@iceland.freeshell.org> writes:
>
>> On 2012-05-31, Lox <skolpojken72@yahoo.se> wrote:
>>> Sorry, two typos (wrote to fast). :-P
>>>
>>> uint32_t x;
>>> uint8_t y;
>>>
>>> x = 255;
>>> y = x + UINT8_C(1);
>>>
>>> y is 256 and not 0 as one would think (if assuming 8 bit
>>> calculations).

>>
>> Again, y can only hold values from 0 to 255 inclusive,
>> so it cannot be 256. How did you come to that conclusion?
>> Can you show the code?

>
> If I'm reading the standard correctly, an unsigned char is large
> enough to hold values from 0 to 255 (actually "any member of the basic
> execution character set"). ...


Actually, the key requirement is that UCHAR_MAX >= 255 (5.2.4.2.1p1).

> ... It doesn't say that it can't be larger than
> that.


While that's all perfectly true, it's not relevant. The type used in
this example was uint8_t, not char. uint8_t is required by the standard
to have exactly 8 values bits, no more, and no less; and no padding bits
(7.20.1.1p2). On any implementation where uint8_t can be implemented,
it's likely (but not required) to be the same type as unsigned char.
However, uint8_t is an optional type, for precisely the reason that it
cannot be implemented as specified on any implementation where CHAR_BIT > 8.
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #17 (permalink)  
Old 05-31-2012, 06:11 PM
Joe Pfeiffer
Guest
 
Posts: n/a
Default Re: Why, warning: comparison between signed and unsigned integer expressions?

James Kuyper <jameskuyper@verizon.net> writes:

> On 05/31/2012 01:09 PM, Joe Pfeiffer wrote:
>> Ike Naar <ike@iceland.freeshell.org> writes:
>>
>>> On 2012-05-31, Lox <skolpojken72@yahoo.se> wrote:
>>>> Sorry, two typos (wrote to fast). :-P
>>>>
>>>> uint32_t x;
>>>> uint8_t y;
>>>>
>>>> x = 255;
>>>> y = x + UINT8_C(1);
>>>>
>>>> y is 256 and not 0 as one would think (if assuming 8 bit
>>>> calculations).
>>>
>>> Again, y can only hold values from 0 to 255 inclusive,
>>> so it cannot be 256. How did you come to that conclusion?
>>> Can you show the code?

>>
>> If I'm reading the standard correctly, an unsigned char is large
>> enough to hold values from 0 to 255 (actually "any member of the basic
>> execution character set"). ...

>
> Actually, the key requirement is that UCHAR_MAX >= 255 (5.2.4.2.1p1).
>
>> ... It doesn't say that it can't be larger than
>> that.

>
> While that's all perfectly true, it's not relevant. The type used in
> this example was uint8_t, not char. uint8_t is required by the standard
> to have exactly 8 values bits, no more, and no less; and no padding bits
> (7.20.1.1p2). On any implementation where uint8_t can be implemented,
> it's likely (but not required) to be the same type as unsigned char.
> However, uint8_t is an optional type, for precisely the reason that it
> cannot be implemented as specified on any implementation where CHAR_BIT > 8.


Ah. I was looking for the string uint8_t in the standard and not
finding it... You are correct.
Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 08:44 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.