Skip to content
Physics and Astronomy
Home Our Teaching Resources C programming Appendix on integers
Back to top
On this page
Contents

Appendix: more details about integers

The occasional appendices and optional examples in this module are for advanced material that you will not need for this module. They are intended for enthusiastic students who are interested in going further in programming for its own sake.

Length of integer variables

C is designed to run on anything from a tiny one-chip micro-controllers with a few thousand bytes of memory (or less!) upwards and so integer variables are available that use anything from one to eight bytes each. One or two-byte integers are typically used for systems with very little memory.

Most systems default to four-byte integers but provide for eight-byte integers when rewuired. Examples where we would need to use an eight-byte integer include:

  • Bank accounts are considered to contain a whole number of cents. A four-byte int would only allow balances of plus or minus  2 billion cents or pence, i.e. approximately plus or minus $20,000,000.That's enough for you and me but not for a medium-sized company.
  • If we wish to number the bytes in a file 0, 1, 2... etc. then using an unsigned int would limit us to a maximum file length of 4GiB. Indeed, it's not that long ago that this was the case. Using an unsigned long int allows a file length of several exabytes.
  • A similar problem related to computer memory or RAM. Computer operating systems and applications need to be able to number their memory by bytes (byte number zero, byte number 1, byte number 2, etc.) Older, 32-bit, processors used 32 bits to store a memory address and were therefore limited applications to using 4GiB of RAM. For that reason, all modern PC processors are 64-bit, although most will still run 32-bit code

Length of integer types

Unfortunately the numbers of bytes per type is not standardised, the only guarantee being that each type has a certain minimum length:

Type Minimum bytes Typical
char
1
1
short int
2
2
int
2
4
long int
4
4 or 8
long long int
8
8

NB "short int" can just be abbreviated to "short", "long int" to "long",etc. If you are really need to know the length of your system's integer types then this size checking program will tell you.

Division of negative integers

In the main notes, we very slightly skipped over division of negative integers because they are hardly ever used. However...

We know that 5/2 equals 2 but should -5/2 be -2 or -3?

  1. -5/2, equals -2 rounds towards zero and preserves the equality -j/k equals -(j/k)
  2. -5/2 equals -3 preserves the fact that "integer" j/k is always less than or equal to "floating-point" j/k, giving a positive remainder j % k.

The fact that negative-integer division and remainder used not to be uniquely defined shows just how rare it is.

Early versions of C allowed the compiler to choose either option (!); that choice has now been removed and the rule is now option (i): negative numbers round towards zero:
   -5/2 equals 5/-2 equals -2
   -j/k equals j/-k equals -(j/k)

   -5 % 2 equals 5 % -2 equals -1
   -j % k equals j % -k equals -(j % k)

Floating-point to integer conversion

C adopts exactly the same policy when dealing with statements like:

  x = 1.82;
  j = x;

the whole point of integers is that they are not allowed to have fractional values so j is assigned the value 1. Again note that this is not necessarily the nearest integer.

Negative values are also handled in the same way as integer division, rounding towards zero:

  x = -1.82;
  j = x;

gives j the value of -1;

Finally, if we do ever find ourselves converting floating-point expressions to integers we must be be aware that rounding errors can tip the result by one from the mathematically correct result when that result is exactly, or close to, an integer

                                                                                                                                                                                                                                                                       

Validate   Link-check © Copyright & disclaimer Privacy & cookies Share
Back to top