Cloudbook: C

  1. Home
  2. Variables
  3. Numbers
  4. § 2 exercise →
Numbers

The integer types short, int and long (as well as the C99 long long) are either further typed as signed or unsigned. The unsigned types need to be declared unsigned, as signed types are the default: e.g. unsigned short, unsigned int, or unsigned long.

Unsigned types represent integers from 0 up to a positive maximum. All integer arithmetic is in fact modular arithmetic. If M is the largest representable number in a given unsigned type, then it is true that

        	assert( (M+1)==0 )
        
The maximum is given by the number of bits in the location storing the value. For k bits, their are 2k possible values, so those values can be arranged to count from 0 up to 2k-1. Hence 2k-1 is the maximum for an unsigned integer type of k bits. For example, 4 Gi = 4,294,967,295 for a 32 bit unsigned int.

An example of an unsigned long is the return value of the sizeof operator. To use sizeof properly in expressions, it is important to remember it is an unsigned quantity.