You should first check that an unsigned long
is 32 bits. If you're getting values above about (roughly) 4.2 billion, it's almost certainly wider than that(a).
You can check this by compiling and running the following program:
#include <limits.h>
#include <stdio.h>
int main(void) {
printf("%d\n%zu\n", CHAR_BIT, sizeof(unsigned long));
return 0;
}
The first value is the number of bits in a byte, the second the number of bytes in an unsigned long
. Multiplying the two will therefore give you the number of bits in the unsigned long
type.
On my system, I get 8
and 8
, indicating a 64-bit size.
(a) The ISO C standard does not mandate an exact size for the original types found in C (though it may for things like uint32_t
). In fact it doesn't directly even mandate the number of bits at all.
What is does mandate is the minimum range requirements which, for unsigned long
is 0..4294967295
(the 4.2 billion I mentioned before).
However, an implementation is free to provide you with something larger, such as a 128-bit type, which would give you a range from zero up to about 1038, or a hundred million million million million million million.
As an aside, I could have used billions, trillions, or even quadrillions but:
- there's sometimes disagreement as to that actual powers of ten they represent; and
- the use of many "million" suffixes drives home the size more than a single rarely knows suffix like "undecillion" or "sextillion".