Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!usc!ginosko!aplcen!haven!adm!smoke!gwyn
From: gwyn@smoke.BRL.MIL (Doug Gwyn)
Newsgroups: comp.std.c
Subject: Re: subtraction between unsigned ints
Message-ID: <10948@smoke.BRL.MIL>
Date: 6 Sep 89 10:22:38 GMT
References: 
Reply-To: gwyn@brl.arpa (Doug Gwyn)
Organization: Ballistic Research Lab (BRL), APG, MD.
Lines: 23

In article  kuro@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.