Cloudbook: C

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

The C Language types that take values similar to numbers are the integral types of signed or unsigned char, short, int and long; and the floating point types of float and double.

All integer types have only a finite number of values. They are therefore model mathematical integers only approximately, as there are in infinite number of integer values. The difference between char, short, long and long long is the number of bytes in the representation, hence the range of values representable in the type.

The sizeof operator can be applied to a type or variable and gives the number of bytes in a location that stores that type.

C Language guarantees that a char will be one byte. As such, it is the most flexible type for manipulating memory directly. In order that conversions have reasonable outcomes, a type of lesser range can be cast to a type of greater range without loss of values. Which means that:

        1 = sizeof(char) 
          <= sizeof(short) 
          <= sizeof(int) 
          <= sizeof(long) 
        

It is almost assured that sizeof(short) is two, that is, that shorts are 16 bits. This is an important integer length since the Unicode character set, or Microsoft's wide chard (wchar) are 16 bits. However, the C standard permits shorts to be longer. For the purposes of this class, the statement:

        assert(sizeof(short)==2) ;
        
is sufficient precaution.

For 64 bit machines, it is possible that the long type is 64 bits, and the int type is 32 bits. This is called the LP64 model, and is common; but it cannot be relied upon. There was some call for int and long both being 64 bits, which is termed the ILP64 model, and for int and long both being 32 bits, which is termed the LLP64 model.

The LLP64 model requires the introduction of a new type, the long long, which would be 64 bits. This was added in C99, and I think this is all a very bad idea. It is part of C99's philosophy that a bad C program will be improved by matching it with a bad language.

However, only Microsoft seems to have adopted LLP64 for the Win64 API, and the kernel did not follow Win32 when it was 32 bit, and might not be following Win64 now that it is 64 bit.