Advertisement
Advertisement

## Range of values in C Int and Long 32 - 64 bits

### Question

I'm confused with range of values of Int variable in C.

I know that a 32bits unsigned int have a range of: 0 to 65,535. So long has 0 to 4,294,967,295

This is fine in 32bits machine. But now in 64bits machines all thing keep the same? Or maybe my int capacity is different?

I understand this questions as newbie, but I'm really confused. This method signature is not helping too. :)

``````unsigned long long int atomicAdd(unsigned long long int* address, unsigned long long int val);
``````
2011/05/27
1
45
5/27/2011 5:34:16 PM

### Accepted Answer

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.

2011/05/27
70
5/27/2011 5:51:55 PM

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.

2018/01/12

Excerpt from K&R:

`short` is often 16 bits, `long` 32 bits and `int` either 16 bits or 32 bits. Each compiler is free to choose appropriate sizes for its own hardware, subject only to the restriction that `short`s and `int`s are at least 16 bits, `long`s are at least 32 bits, and `short` is no longer than `int`, which is no longer than `long`.

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.

2015/08/02

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.

2011/05/27

In fact, unsigned int on most modern processors (ARM, Intel/AMD, Alpha, SPARC, Itanium ,PowerPC) will have a range of 0 to 2^32 - 1 which is 4,294,967,295 = 0xffffffff because int (both signed and unsigned) will be 32 bits long and the largest one is as stated.

(unsigned short will have maximal value 2^16 - 1 = 65,535 )

(unsigned) long long int will have a length of 64 bits (long int will be enough under most 64 bit Linuxes, etc, but the standard promises 64 bits for long long int). Hence these have the range 0 to 2^64 - 1 = 18446744073709551615

2011/05/27

In C and C++ memory requirements of some variable :

`signed char: -2^07 to +2^07-1`
`short: -2^15 to +2^15-1`
`int: -2^15 to +2^15-1`
`long: -2^31 to +2^31-1`
`long long: -2^63 to +2^63-1`

`signed char: -2^07 to +2^07-1`
`short: -2^15 to +2^15-1`
`int: -2^31 to +2^31-1`
`long: -2^31 to +2^31-1`
`long long: -2^63 to +2^63-1`

depends on compiler and architecture of hardware

The international standard for the C language requires only that the size of short variables should be less than or equal to the size of type int, which in turn should be less than or equal to the size of type long.

2013/07/11

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]