subtraction between unsigned ints

Doug Gwyn gwyn at smoke.BRL.MIL
Wed Sep 6 20:22:38 AEST 1989


In article <KURO.89Sep5111940 at shochu.Sun.Com> kuro at shochu.Sun.Com (Teruhiko Kurosaka - Sun Intercon) writes:
>Does pANSI defines the datatype of the result of subtraction
>between unsigned integers?  Would it be also unsigned?

Yes, the type of the result of any arithmetic combination of operands
having a single arithmetic type is the same type as that of the operands.

>Then what would be the result of subtraction of a number from a
>smaller number?

Unsigned arithmetic is always modular arithmetic, that is, it is
performed modulo the word size of the unsigned type involved.

>	unsigned int	u, v;
>	long int	x;
>	u=3; v=5;
>	x=u-v;
>Is x guranteed to be -2?

The answer is no.  For a 16-bit implementation, u-v is 65534 and
has type (unsigned int).  Before the assignment is performed, the
operands of the = operator are promoted to a common type (long int
in this particular scenario), and x receives the value 65534.



More information about the Comp.std.c mailing list