Range of values in C Int and Long 32 - 64 bits
Solution 1:
In C and C++ you have these least requirements (i.e actual implementations can have larger magnitudes)
signed char: -2^07+1 to +2^07-1
short: -2^15+1 to +2^15-1
int: -2^15+1 to +2^15-1
long: -2^31+1 to +2^31-1
long long: -2^63+1 to +2^63-1
Now, on particular implementations, you have a variety of bit ranges. The wikipedia article describes this nicely.
Solution 2:
No, int
in C is not defined to be 32 bits. int
and long
are not defined to be any specific size at all. The only thing the language guarantees is that sizeof(char)<=sizeof(short)<=sizeof(long)
.
Theoretically a compiler could make short
, char
, and long
all the same number of bits. I know of some that actually did that for all those types save char
.
This is why C now defines types like uint16_t
and uint32_t
. If you need a specific size, you are supposed to use one of those.
Solution 3:
Excerpt from K&R:
short
is often 16 bits,long
32 bits andint
either 16 bits or 32 bits. Each compiler is free to choose appropriate sizes for its own hardware, subject only to the restriction thatshort
s andint
s are at least 16 bits,long
s are at least 32 bits, andshort
is no longer thanint
, which is no longer thanlong
.
You can make use of limits.h
that contains the definition of the limits for the decimal/float types:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>
int main(int argc, char** argv) {
printf("CHAR_BIT : %d\n", CHAR_BIT);
printf("CHAR_MAX : %d\n", CHAR_MAX);
printf("CHAR_MIN : %d\n", CHAR_MIN);
printf("INT_MAX : %d\n", INT_MAX);
printf("INT_MIN : %d\n", INT_MIN);
printf("LONG_MAX : %ld\n", (long) LONG_MAX);
printf("LONG_MIN : %ld\n", (long) LONG_MIN);
printf("SCHAR_MAX : %d\n", SCHAR_MAX);
printf("SCHAR_MIN : %d\n", SCHAR_MIN);
printf("SHRT_MAX : %d\n", SHRT_MAX);
printf("SHRT_MIN : %d\n", SHRT_MIN);
printf("UCHAR_MAX : %d\n", UCHAR_MAX);
printf("UINT_MAX : %u\n", (unsigned int) UINT_MAX);
printf("ULONG_MAX : %lu\n", (unsigned long) ULONG_MAX);
printf("USHRT_MAX : %d\n", (unsigned short) USHRT_MAX);
printf("FLT_MAX : %g\n", (float) FLT_MAX);
printf("FLT_MIN : %g\n", (float) FLT_MIN);
printf("-FLT_MAX : %g\n", (float) -FLT_MAX);
printf("-FLT_MIN : %g\n", (float) -FLT_MIN);
printf("DBL_MAX : %g\n", (double) DBL_MAX);
printf("DBL_MIN : %g\n", (double) DBL_MIN);
printf("-DBL_MAX : %g\n", (double) -DBL_MAX);
return (EXIT_SUCCESS);
}
Maybe you might have to tweak a little bit on your machine, but it is a good template to start to get an idea of the (implementation-defined) min and max values.
Solution 4:
There's no one answer. The standard defines minimum ranges. An int must be able to hold at least 65535. Most modern compilers however allow ints to be 32-bit values. Additionally, there's nothing preventing multiple types from having the same capacity (e.g. int and long).
That being said, the standard does say in your particular case:
0 → +18446744073709551615
as the range for unsigned long long int.
Further reading: http://en.wikipedia.org/wiki/C_variable_types_and_declarations#Size