18.1.3: unsigned integers

For each of the integral types, there is a corresponding unsigned type. Thus, we have unsigned char, unsigned short int, unsigned int, and unsigned long int. There are two differences between the unsigned types and the default, signed types:

  1. They do not hold negative numbers; their range is from 0 up to some maximum.
  2. They have guaranteed properties on overflow. If the range of unsigned int on some machine is 0-65,535, and if an unsigned int variable contains the value 65,535, then adding 1 to it will wrap around to 0. Whenever a calculation involving unsigned integers overflows, or tries to go negative, the result is the remainder that would be obtained if the true (mathematical) result were divided by the range of the unsigned type. In other words, if UINT_MAX is the largest value that will fit in an unsigned int (that is, if the range is 0-UINT_MAX), then the results of calculations that would overflow, such as
    	65535 + 1
    	5 - 10
    are actually
    	(65535 + 1) % (UINT_MAX+1)
    	(5 - 10) % (UINT_MAX+1)

The guaranteed minimum ranges of the unsigned types are:

	unsigned char	0 - 255
	unsigned short int	0 - 65535
	unsigned int	0 - 65535
	unsigned long int	0 - 4294967295

These multiword type names can also be abbreviated. Instead of writing long int, you can write long. Instead of writing short int, you can write short. Instead of writing unsigned int, you can write unsigned. Instead of writing unsigned long int, you can write unsigned long. Instead of writing unsigned short int, you can write unsigned short.

In the absence of the unsigned keyword, types short int, int, and long int are all signed. However, depending on the particular compiler you're using, plain type char might be signed or unsigned. If you need an explicitly signed character type, you can use signed char.

Read sequentially: prev next up top

This page by Steve Summit // Copyright 1996-1999 // mail feedback