What is the function to determine the min and max possible of value of datatypes (i.e, int, char.etc) in C?
Maximum value of any unsigned integral type:
((t)~(t)0)
// Generic expression that would work in almost all
circumstances.
(~(t)0)
// If you know your type t
have equal or larger size than
unsigned int
. (This cast forces type promotion.)
((t)~0U)
// If you know your type t
have smaller size than
unsigned int
. (This cast demotes type after the unsigned int
-type
expression ~0U
is evaluated.)
Maximum value of any signed integral type:
If you have an unsigned variant of type t
, ((t)(((unsigned t)~(unsigned t)0)>>1))
would give you the fastest result you need.
Otherwise, use this (thanks to @vinc17 for suggestion): (((1ULL<<(sizeof(t)*CHAR_BIT-2))-1)*2+1)
Minimum value of any signed integral type:
You have to know the signed number representation of your machine. Most machines use 2's complement, and so -(((1ULL<<(sizeof(t)*CHAR_BIT-2))-1)*2+1)-1
will work for you.
To detect whether your machine uses 2's complement, detect whether (~(t)0U)
and (t)(-1)
represent the same thing.
So, combined with above:
(-(((1ULL<<(sizeof(t)*CHAR_BIT-2))-1)*2+1)-(((~(t)0U)==(t)(-1)))
will give you the minimum value of any signed integral type.
As an example: Maximum value of size_t
(a.k.a. the SIZE_MAX
macro) can be defined as (~(size_t)0)
. Linux kernel source code define SIZE_MAX
macro this way.
One caveat though: All of these expressions use either type casting or sizeof
operator and so none of these would work in preprocessor conditionals (#if
... #elif
... #endif
and like).
(Answer updated for incorpoating suggestions from @chux and @vinc17. Thank you both.)