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
Solution 5
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.
Related videos on Youtube
Custodio
Full stack developer living in London I’ve been working as a full stack developer for quite a while, going from db to front end and devops operations.
Updated on July 09, 2022Comments
-
Custodio almost 2 years
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);
-
Will A almost 13 years32-bit unsigned integers have a max value of 2^32 - 1, which is way more than 65535 (2^16 - 1).
-
-
Custodio almost 13 yearsNice, can you say that is the
long long int
definition in method signature of question? -
Johannes Schaub - litb almost 13 years@Custodio you used
unsigned long long
, which means you have a range of at least0 .. 2^64-1
. -
Custodio almost 13 yearsNice, this signature is from a framework that I'm using, And I'm having problem calling the AtomicAdd
-
mg007 almost 11 yearsSorry for commenting after a long time, but I have a feeling that C follows 2's complement arithmetic, where values lie in [-2^(N-1), 2^(N-1)-1] for N-bit data type. Can you please verify this (No extra 1 in lower endpoint)?
-
interjay almost 11 yearsThese values are wrong. The values given in Johannes Schaub's answer, which you have attempted to correct, are the actual values given by the standard as the minimum required to be supported by an implementation.
-
glerYbo almost 10 yearsHow about
long double
(double
)? -
CᴴᴀZ almost 10 yearsThat's why I get
sizeof(int)
=4 andsizeof(long)
=4 on my Linux system (GCC). Since, there's no specific size but a min size instead. -
sumanth232 about 9 yearsThe ranges are clearly wrong. long: -2^31 to +2^31-1
-
Johannes Schaub - litb about 9 years@krishna222 it is not wrong. the limits are drafted such that not only 2's complement can be used, but also one's complement and sign magnitude. therefore the least lower limit is
-2^31+1
rather than-2^31
(which would end up as -0 for sign magnitude on 32 bits..). -
sumanth232 about 9 years@JohannesSchaub-litb, but C , C++ follows 2 compliement, right ? int x = 0x80000000; printf("x = %d", x); -> prints x = -2147483648 which is equal to -2^31
-
Martin Bonner supports Monica over 8 yearsSpecifically, signed char is NOT required to support -2^7 (-128). It only has to cope with -127. Two's complement machines will support -128, but sign-and-magnitude will only go down to -127 (which is exactly why the limit is what it is). Similarly for the other types
-
Martin Bonner supports Monica over 8 yearsIt also guarantees some minimum sizes.
-
Sнаđошƒаӽ over 8 yearsNice excerpt from K&R! And +1 for the nice coat! Wait, did I say coat? Well, you got that right!
-
einpoklum about 8 yearsUmm, isn't it a C requirement that sizeof(char) = 1? And since there must be an
int8_t
type, does that not mean it is also effectively a requirement thatCHAR_BITS == 8
? Also, AFAICR Posix requiresCHAR_BIT == 8
anyway. Meaning that it is not OK for a compiler to makechar
andshort
the same type. Although I guessshort
=int
=long
is OK. -
T.E.D. about 8 years@einpoklum - According to what I can dig up, CHAR_BITS must be at least 8, but may be more. But that's today, if your compiler happens to be conformant to that particular standard. Back in '11 when I wrote this, I still occasionally had to work on systems with K&R'ish compilers. In a world chock full of non-standard compilers, quoting today's standard isn't the end of the discussion.
-
einpoklum about 8 yearsBut it's not "perfectly OK", it's against the C99 standard, which is almost 20 years old already. So - would suggest an edit.
-
T.E.D. about 8 years@einpoklum - I'll rephrase that, since the "OK" seems to bug people so much. But I think lots of people think things are "standard" that are not. There's a reason they made those sized types (including int8_t and uint8_t). I'm pretty sure they can indeed legit all be the same size.
-
T.E.D. about 8 years@einpoklum ...note that the inequality relation in my answer, including the part about char, can be found nearly verbatim in this OpenGroup whitepaper about C data sizes on 64-bit systems.
-
Sagar over 7 years@Elyasin How can I find the limits for long Double which is for 80 bit ? Any suggestions ?
-
Ely over 7 yearsI did not list it, but you should find it also in the
float.h
file if it is defined. -
M.M over 7 yearsThere are also guarantees that certain ranges of values can be represented in each type; see the top-voted answer
-
M.M over 7 years@einpoklum
int8_t
is not guaranteed to exist in ISO C. The intNN_t types are all optional and if the implementation cannot satisfy the requirements then it will not offer those types. -
M.M over 7 yearsThe casts on integer types are redundant at best (C11 5.2.4.2.1/1)
-
M.M over 7 yearsAn
int
must be able to hold at least 32767. It might not be able to hold 65535. On typical 16-bit implementations it wouldn't. -
T.E.D. over 6 years@chux - I suppose that might be a useful clarification on platforms where unsigned and signed types are different sizes. Are there any such platforms?
-
chux - Reinstate Monica over 6 years@T.E.D. I apologize - I mis-thought as
sizeof(char)<=sizeof(short)
meaningCHAR_MAX <= SHRT_MAX
, which might not be true where asUCHAR_MAX <= SHRT_MAX
is certainly true.