Integer types, and integer overflows, in C
Archive - Originally posted on "The Horse's Mouth" - 2012-01-25 06:59:42 - Graham EllisC offers you the ability to define a whole variety of integer types:
short int my_number;
int my_number;
long int my_number;
typically occupying 16, 32 and 64 bits of memory (but that is NOT a standard, and you may find different on some systems). And the amount of memory that's declared controls the minimum and maximum numbers that can be held. C, as a language which assumes the programmer knows what (s)he is doing, will not check for overflow for you, and will give results which are plain wrong arithmetically if you exceed the limits.
There's an example [here] from yesterday's C course, where I attempted to fill an array with an exponentially growing series of numbers, and ended up bursting even the capability of a long int. You'll note that (on my machine, in this case), I ended up getting zeros back rather than the largest number the machine can handle - a dramatic failure of my code.
There's a header file - limit.h - which #defines a whole load of constants such as INT_MAX to help you program to check for limits. There's an example of it in use [here] in a training example from our C Programming course.