Bingo! see below...

On Thu, Apr 22, 2010 at 01:07:40PM -0700, Bakul Shah wrote:
> On Thu, 22 Apr 2010 21:32:36 +0200 tlaro...@polynum.com  wrote:
> > On Thu, Apr 22, 2010 at 03:08:40PM -0400, ge...@plan9.bell-labs.com wrote:
> > > What type is `smallnumber'?
> > 
> > typedef unsigned char smallnumber; 
>           ^^^^^^^^
> Aha!
> 
> > translated from Pascal:
> > 
> > small_number=0..63;
> 
> IIRC in C89 integer promotions rules changed.  See 6.3.1.8
> (Usual arithmetic conversions)
> 
>             [Otherwise,]the integer promotions are performed on both
>             operands.   Then the following rules are applied to the
>             promoted operands:
> 
>                  If both operands  have  the  same  type,  then  no
>                  further conversion is needed.
> 
>                  Otherwise,  if  both  operands have signed integer
>                  types or both have  unsigned  integer  types,  the
>                  operand with the type of lesser integer conversion
>                  rank is converted to the type of the operand  with
>                  greater rank.
> 
>                  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.
> 
> >>>>             Otherwise,  if the type of the operand with signed
>                  integer type can represent all of  the  values  of
>                  the  type  of  the  operand  with unsigned integer
>                  type, then the operand with unsigned integer  type
>                  is  converted  to  the  type  of  the operand with
>                  signed integer type.
> 
>                  Otherwise, both  operands  are  converted  to  the
>                  unsigned integer type corresponding to the type of
>                  the operand with signed integer type.
> 
> Try this on both gcc and 8c (with suitable changes):
> 
> #define N(i) atoi(v[i])
> int f(int x, int y, unsigned char z) { return (x + y + z) / 2; }
> int main(int c, char**v) { printf("%d\n", f(N(1), N(2), N(3))); }

I get:

gcc: -201
ken-cc: 2147483447 (2^31 - 201)

This is: signed long + signed long + unsigned char.

Do you mean that there is first promotion :

        1) unsigned char is promoted to unsigned int (A6.1).

        2) And since there is an arithmetic operator (/ or shift), 
        unsigned int value may exceeds signed long == signed int, the 
        signed long is converted to unsigned long (A6.2)?

Is it this?!!!!?

And when I do first assignment, there is only promotion (since no
operator is here). Yielding the correct value in x2, that is then
divided (or shifted) by 2, hence signed, and no problem?

Well, thanks for the lessons, yesterday, today and tomorrow...;)
-- 
        Thierry Laronde <tlaronde +AT+ polynum +dot+ com>
                      http://www.kergis.com/
Key fingerprint = 0FF7 E906 FBAF FE95 FD89  250D 52B1 AE95 6006 F40C

Reply via email to