E
Eyegor
Hi all,
I wonder what would be the best way to determine the maximum integer
value a variable can store if I do not know in advance the type of a
variable?
I have the following declarations:
/*Defines*/
#define AEI_unit_max 255//4294967295 /*Largest integer representable
by 32 bit AEI_unit*/
/*Type Definitions*/
typedef unsigned long AEI_unit;
I do not know in advance what the user will choose to use for
AEI_unit. common choices are 8/16/32 bit unsigned integers.
Also I do not know the target compiler, 32-bit or 64-bit compilers. I
know that different compilers treat variable differently, see:
http://en.wikipedia.org/wiki/Limits.h
my question is this:
What is the best way to assign a value to AEI_unit_max based on
AEI_unit type?
I tried to declare AEI_unit_max as a variable rather than a #define
and do the following assignment:
AEI_unit_max=(AEI_unit)(-1);
But this does not seem to work, i get gibberish from calculations
which use AEI_unit_max value.
I also tried AEI_unit_max=~0; but again no luck. Are these two methods
compiler/implementation depended? Is there a more elegant way to do
this?
Is including limits.h and using if->then statements with
sizeof(AEI_unit) be the most sure way?
Thanks
I wonder what would be the best way to determine the maximum integer
value a variable can store if I do not know in advance the type of a
variable?
I have the following declarations:
/*Defines*/
#define AEI_unit_max 255//4294967295 /*Largest integer representable
by 32 bit AEI_unit*/
/*Type Definitions*/
typedef unsigned long AEI_unit;
I do not know in advance what the user will choose to use for
AEI_unit. common choices are 8/16/32 bit unsigned integers.
Also I do not know the target compiler, 32-bit or 64-bit compilers. I
know that different compilers treat variable differently, see:
http://en.wikipedia.org/wiki/Limits.h
my question is this:
What is the best way to assign a value to AEI_unit_max based on
AEI_unit type?
I tried to declare AEI_unit_max as a variable rather than a #define
and do the following assignment:
AEI_unit_max=(AEI_unit)(-1);
But this does not seem to work, i get gibberish from calculations
which use AEI_unit_max value.
I also tried AEI_unit_max=~0; but again no luck. Are these two methods
compiler/implementation depended? Is there a more elegant way to do
this?
Is including limits.h and using if->then statements with
sizeof(AEI_unit) be the most sure way?
Thanks