[comp.lang.c] The use of unsigned int

paul@tredysvr.Tredydev.Unisys.COM (Paul Siu) (02/09/91)

The most commonly use, and commonly returned type in C is probably int.  
In some cases, wouldn't it be more appropriate if unsigned int was used
instead, such as when you are indexing an array, or returning a length?

I heard that int is usually declared as the most efficent type in C, would
using unsigned int cause any problems such as slow things down or cause
type-conversation problems?

Paul Siu
paul@tredysvr.tredydev.unisys.com

dave@cs.arizona.edu (Dave P. Schaumann) (02/09/91)

In article <1255@tredysvr.Tredydev.Unisys.COM> paul@tredysvr.Tredydev.Unisys.COM (Paul Siu) writes:
>The most commonly use, and commonly returned type in C is probably int.  
>In some cases, wouldn't it be more appropriate if unsigned int was used
>instead, such as when you are indexing an array, or returning a length?
>
>I heard that int is usually declared as the most efficent type in C, would
>using unsigned int cause any problems such as slow things down or cause
>type-conversation problems?

There is no general answer to this question.  On a twos-complement machine,
the instructions generated for int/unsigned int (of the same size) should
be virtually identical.  The only differences would be in checking for overflow
and checking for sequence (ie less/greater).  Even here, I would expect the
differences in instruction execution speed would be very small, if not 0.

I've never written assembly code on a sign/magnatude machine, so I don't know
about those.  (Are there any 1's complement machines out there?)

>Paul Siu
>paul@tredysvr.tredydev.unisys.com


-- 
Dave Schaumann      | DANGER: Access hole may tear easily.  Use of the access
		    | holes for lifting or carrying may result in damage to the
dave@cs.arizona.edu | carton and subsequent injury to the user.

steve@taumet.com (Stephen Clamage) (02/10/91)

paul@tredysvr.Tredydev.Unisys.COM (Paul Siu) writes:

>The most commonly use, and commonly returned type in C is probably int.  
>In some cases, wouldn't it be more appropriate if unsigned int was used
>instead, such as when you are indexing an array, or returning a length?

>I heard that int is usually declared as the most efficent type in C, would
>using unsigned int cause any problems such as slow things down or cause
>type-conversation problems?

Using unsigned int won't generally speed things up (it might in selected
cases).  The type of variable should be related to what it represents.
Unsigned types are useful for counters and sizes of things.  If you
want the size of an object, use standard type size_t, which is an
unsigned type suitable to measure any object in the system.

With unsigned integer types you do have to be careful about comparisons
near the boundaries of the type and the corresponding signed type.
You can get unintended results:

1.
	unsigned i;
	for( i = 10;  i >= 0;  --i )
	    ...
This loop will never terminate.  An unsigned number can never be negative.

2.
	unsigned u;
	int i;
	...
	if( i < u )
	    ...
In the comparison, i is converted to unsigned, then compared to u.
If i were initially negative, it might now look larger than u.
-- 

Steve Clamage, TauMetric Corp, steve@taumet.com

greywolf@unisoft.UUCP (The Grey Wolf) (02/14/91)

In article <1255@tredysvr.Tredydev.Unisys.COM> paul@tredysvr.Tredydev.Unisys.COM (Paul Siu) writes:
>The most commonly use, and commonly returned type in C is probably int.  
>In some cases, wouldn't it be more appropriate if unsigned int was used
>instead, such as when you are indexing an array, or returning a length?

I've had occasion to use a signed int as an array index before; I don't
think it's that uncommon.  Unsigned ints are warranted in certain places,
but int seems to be more versatile (you don't have to muck about with
whichever value a failed system call returns).  Also, as has been pointed out,
the difference between signed and unsigned int on a two's-complement
machine (most are these days) is purely semantic.

>Paul Siu
>paul@tredysvr.tredydev.unisys.com


-- 
thought:  I ain't so damb dumn! | Your brand new kernel just dump core on you
war: Invalid argument           | And fsck can't find root inode 2
                                | Don't worry -- be happy...
...!{ucbvax,acad,uunet,amdahl,pyramid}!unisoft!greywolf

bright@nazgul.UUCP (Walter Bright) (02/26/91)

In article <1255@tredysvr.Tredydev.Unisys.COM> paul@tredysvr.Tredydev.Unisys.COM (Paul Siu) writes:
/The most commonly use, and commonly returned type in C is probably int.  
/In some cases, wouldn't it be more appropriate if unsigned int was used
/instead, such as when you are indexing an array, or returning a length?

If the values for your variables are never negative, you should declare them
as unsigned. The reasons are:
1.	Documentation
2.	Aid to the optimizer. Certain optimizations are not possible if the
	variable could be negative, such as replacing a divide with a shift
	and replacing a loop index with a pointer.