The unsigned integer data types are
unsigned short int unsigned int unsigned long intunsigned may be written instead of unsigned int . When an integer is declared as unsigned the meaning is that the most significant bit in the internal representation is taken as significant data rather than a sign. All previous comments on signed integers apply. The rules of arithmetic are slightly different for unsigned integers in an obvious way. The main differences concern different input and output conversion codes and the existence of a number of extra operators available to manipulate unsigned values.
The main use of unsigned integers is to provide a convenient way to access the underlying sequence of bits in computer memory. This is important when the bit values are used to control output devices or to determine the status of input devices. Under these circumstances it is common for a program to need to determine the value of a particular bit or to need to set a particular bit or group of bits to particular values. Such operations are sometimes called bit-twiddling.
For input and output the unsigned int conversions u , o , x and X are used. They may be preceded by "l" or "h" for long or short unsigned integers. The "u" conversion is for conversion to or from an unsigned decimal form. The "o" conversion is for conversion to or from an octal representation of a value. The "x" and "X" conversions are for conversion to or from a hexadecimal representation of a value. The "X" conversion uses capital letters to represent the hexadecimal digits greater than 9, the "x" conversion uses lower case letters. Octal representation is widely used in the Unix environment for historical reasons. In the Unix environment the "x" conversion is used in preference to the "X" conversion. These conversions may be used with field specifications in exactly the same way as the "d" conversion seen earlier.
There is no output conversion for binary notation. The library functions strtol() and strtoul() which are discussed later provide facilities for input of binary values via strings. There is no way of writing a binary constant. You have to accept the grouping of bits in threes or fours implicit in octal or hexadecimal notation.
Where appropriate numerical values or constants can be written using octal or hexadecimal notation. Such constants are always unsigned but may be included in normal arithmetic expressions as a consequence of the rules for evaluating expressions involving objects of different data types.
An octal constant is a sequence of the digits 0-7 with an initial 0. A hexadecimal constant is a sequence of the characters 0-9,A-F,a-f with an initial 0x or 0X. Note that this means that any numerical constant with an initial or leading zero is not interpreted as a decimal constant. An unsigned decimal constant may be written by putting a "U" or "u" symbol at the end of the constant.
The following program shows the use of octal and hexadecimal constants and conversions.
main()
{
unsigned int x = 0300; /* octal constant */
unsigned int y = 300U; /* decimal constant */
unsigned int z = 0x300; /* hexadecimal constant */
/* output using unsigned decimal conversion */
printf("unsigned decimal x = %4u y = %4u z = %4u\n",x,y,z);
/* output using octal conversion */
printf(" octal x = %4o y = %4o z = %4o\n",x,y,z);
/* output using hexadecimal conversion */
printf(" hexadecimal x = %4x y = %4x z = %4x\n",x,y,z);
}
producing the output
unsigned decimal x = 192 y = 300 z = 768
octal x = 300 y = 454 z = 1400
hexadecimal x = c0 y = 12c z = 300