...
>>> void test(void)
>>> {
>>> uint8_t a;
>>> uint32_t i;
>>>
>>> a = 10;
>>>
>>> for(i = 0; i < (a + 1); i++)
>>> {
>>>
>>> }
>>> }
>>
>> Integer promotions.
>> In i < (a + 1), a is promoted to an int because, apparently on your
>> implementation, int can represent all values of type uint8_t.
>
> So all the discussion in the recent thread ("condition true or false? ...")
> about mixed arithmetic being unsigned, isn't always true?
A lot of things were said in that discussion by people with varying
levels of understanding of the language. What actually happens depends
upon the usual arithmetic conversions, which start with the integer
promotions. The assertion some participants made that "mixed arithmetic
is unsigned" was, at best, a simplification that becomes false under two
circumstances:
1. If all possible values of the unsigned type can be represented as an
'int', the unsigned value is promoted to an 'int'. The same rule applies
to the signed type. The expression is evaluated using whichever promoted
type has the higher integer conversion rank.
2. If the expression still has mixed signedness after the integer
promotions, the following rule applies to the promoted types of the
operands. If the unsigned type and has an integer conversion rank less
than that of the signed type, and all values of the unsigned type can be
represented in the signed type, then the expression is evaluated using
the signed type.
> (Yes I've now read 6.3.1.8. But why would C care more about preserving
> possible negative values of a wider int, than the negative values of a
> narrower int? The mixed arithmetic rule could have been more consistent.)
One key concept in C is that 'int' and 'unsigned int' were chosen by the
implementation to be the natural type for ordinary integer arithmetic.
Therefore, C requires promotion to one of those two types, if the result
is value preserving, before anything other aspect of the usual
arithmetic conversions are applied. It is consistent application of that
rule that produces the result that you consider inconsistent.
--
James Kuyper