views:

83

answers:

3
 main()  {   
if ( -1 < (unsigned char) 1 )
   printf("less than");
else        
   printf("NOT less than");
  } 

what is happening?

(unsigned char)1 converted to (signed char)1

then: (signed)-1 < (signed)1 thus answer "less than"

problem:- in above code change if ( (-1 < (unsigned int) 1 )

answer "NOT less than".

So its obvious that when I change unsigned char to unsigned int..:-

  • (signed)-1 is converted to unsigned int [exactly opposite is happening]
  • since -1 is stored as 2's compliment of 1; the bit-pattern is evaluated as 255(probably)
  • thus 255 < 1 will evaluate to false and else will execute.
  • even if you substitute int a = -1; in place of '-1' same result

Question:-

1>during signed and unsigned arithmetic...how to be sure if signed will be converted to unsigned or vice versa.

2>why is conversion different for arithmetic between

unsigned char and char : apparently unsigned is converted to signed

and unsigned int and int : apparently signed is converter to unsigned

PS: I know this is not compiler dependent..so don't say it is.

+6  A: 

The rules are as follows:

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:

  1. If both operands have the same type, then no further conversion is needed.
  2. 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.
  3. 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.
  4. 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.
  5. Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.

The rules then work as follows:

  • -1 < (unsigned char) 1

First both operands are converted to ints (because an int can represent all values of unsigned char). Then the comparison is made on these signed types. Rule 1 is then used. The comparison succeeds.

  • -1 < (unsigned int) 1

An int cannot represent all the values of an unsigned int so rule 3 is used and the signed integer is converted to an unsigned integer (UINT_MAX - 1). The comparison now fails.

Mark Byers
"rank" ? what does it implies?
bakra
@bakra: You can think of it as an "ordering" of the types. A long int has a higher rank than a char, for example. See 6.3.1.1 for the official definition of rank.
Mark Byers
@bakra: "rank" of an integral type is defined elsewhere in the C Standard, but the gist of it is which type has bigger range of possible values, plus a few exceptions. In particular, "The rank of a signed integer type shall be greater than the rank of any signed integer type with less precision." and "The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type, if any."
aschepler
again...does "Rank" implies int or char (without caring for signed or unsigned)...re-phrasing: does rank imply how-much-storage the type uses without caring for its value(signed or unsigned)...where can I find the 6.3.1.1 thing?
bakra
@bakra: http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf
Mark Byers
@Mark Byers: almost. In this case, it's due to integer promotions, note the part you omitted from the standard: "Otherwise, the integer promotions are performed on both operands. Then thefollowing rules are applied to the promoted operands:"
ninjalj
Prior to evaluating those numbered bullets, each type less in rank than `int` is converted to `int`, or to `unsigned int` if `int` cannot store its value. So prior to evaluating all these numbered bullets, both operands already are of type `int`, and bullet one is chosen. Lik @ninjalj says.
Johannes Schaub - litb
UINT_MAX - 1, not INT_MAX - 1.
Stephen Canon
@Stephen Canon: Thanks, fixed!
Mark Byers
A: 

operand with signed integer type can represent all of the values of the type of the other operand

can u give an eg. of such a case?

like

int a;

unsigned int b;

bakra
also an eg. of where signed bit "cannot" represent all values of the other?
bakra
+1  A: 

This is due to integer promotions. Both arguments can be represented as an int, so they are converted to an int.

ISO C 6.3.1.1, paragraph 2:

If an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions.48) All other types are unchanged by the integer promotions.

ninjalj