J
John Reye
Hi,
is it possible, to determine the 'type' of a variable, if only its
name is given.
With 'type', I don't mean the exact type, such as enum, but instead
the following information:
a) sizeof
b) signed or unsigned
a) ... is always easy to determine: sizeof(var) !!
b) ... is - as far as I know - only easy to determine, if sizeof(var)
Here are the macros I use:
#define WIDTH_GE_INT(EXPR) (sizeof(EXPR) >= sizeof(int))
#define EXPR_OR_VAR_IS_SIGNED(EXPR)
(WIDTH_GE_INT(EXPR) \
?
P99_SIGNED(EXPR) \
: /* EXPR is a variable */
/* GCC extension - "statement
expressions" */
({int _temp; int _is_signed; _temp = EXPR , \
_is_signed = (!((EXPR = -1) > 0)) , \
EXPR = _temp , \
_is_signed;}))
Get Jens Gustedt's fantastic P99_SIGNED macro here:
http://p99.gforge.inria.fr/p99-html/group__integers_ga3eb39ccac28ebd8265c1a31dc00f53ab.html
As you can see above, if sizeof(var) < sizeof(int), I actually have to
access the variable in order to determine if it is signed or unsigned.
I really do not like this.
Is there any other way, to determine if a variable var is signed or
unsigned, if sizeof(var) < sizeof(int) ???
Thanks.
J.
PS: Is it possible, to write the above macros in standard C, without
using gcc's extension of "statement expressions"
is it possible, to determine the 'type' of a variable, if only its
name is given.
With 'type', I don't mean the exact type, such as enum, but instead
the following information:
a) sizeof
b) signed or unsigned
a) ... is always easy to determine: sizeof(var) !!
b) ... is - as far as I know - only easy to determine, if sizeof(var)
= sizeof(int)
Here are the macros I use:
#define WIDTH_GE_INT(EXPR) (sizeof(EXPR) >= sizeof(int))
#define EXPR_OR_VAR_IS_SIGNED(EXPR)
(WIDTH_GE_INT(EXPR) \
?
P99_SIGNED(EXPR) \
: /* EXPR is a variable */
/* GCC extension - "statement
expressions" */
({int _temp; int _is_signed; _temp = EXPR , \
_is_signed = (!((EXPR = -1) > 0)) , \
EXPR = _temp , \
_is_signed;}))
Get Jens Gustedt's fantastic P99_SIGNED macro here:
http://p99.gforge.inria.fr/p99-html/group__integers_ga3eb39ccac28ebd8265c1a31dc00f53ab.html
As you can see above, if sizeof(var) < sizeof(int), I actually have to
access the variable in order to determine if it is signed or unsigned.
I really do not like this.
Is there any other way, to determine if a variable var is signed or
unsigned, if sizeof(var) < sizeof(int) ???
Thanks.
J.
PS: Is it possible, to write the above macros in standard C, without
using gcc's extension of "statement expressions"