Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Determining the 'type' of a variable, if only its name is given

Reply
Thread Tools

Determining the 'type' of a variable, if only its name is given

 
 
John Reye
Guest
Posts: n/a
 
      06-15-2012
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)
>= 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/...dc00f53ab.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"
 
Reply With Quote
 
 
 
 
John Reye
Guest
Posts: n/a
 
      06-15-2012
Ahhh!

Here's a rewrite of the previous post, with BETTER FORMATTING:




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)>= 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/...eb39ccac28ebd8...

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"
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      06-15-2012
On 06/15/2012 04:51 AM, John Reye wrote:
....
> Get Jens Gustedt's fantastic P99_SIGNED macro here:
> http://p99.gforge.inria.fr/p99-html/...eb39ccac28ebd8...


That link doesn't work for me.
--
James Kuyper
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      06-15-2012


"James Kuyper" <(E-Mail Removed)> wrote in message
news:jrf95p$2dt$(E-Mail Removed)...
> On 06/15/2012 04:51 AM, John Reye wrote:
> ...
>> Get Jens Gustedt's fantastic P99_SIGNED macro here:
>> http://p99.gforge.inria.fr/p99-html/...eb39ccac28ebd8...

>
> That link doesn't work for me.


Try the one in the original post.

(Ie.
http://p99.gforge.inria.fr/p99-html/...dc00f53ab.html)

--
bartc




 
Reply With Quote
 
Jens Gustedt
Guest
Posts: n/a
 
      06-15-2012
Am 15.06.2012 10:51, schrieb John Reye:
> is it possible, to determine the 'type' of a variable, if only its
> name is given.


[snip things about oldish C99]

> Is there any other way, to determine if a variable var is signed or
> unsigned, if sizeof(var) < sizeof(int) ???


with C11's _Generic you may do that, provided your implementation
hasn't extended integer types that are narrower than int,

you can do something horrible like

_Generic(var,
default: use_the_previous_method(var),

char: (CHAR_MAX < UCHAR_MAX),
char const: (CHAR_MAX < UCHAR_MAX),
char volatile: (CHAR_MAX < UCHAR_MAX),
char const volatile: (CHAR_MAX < UCHAR_MAX),
char _Atomic: (CHAR_MAX < UCHAR_MAX),
char const _Atomic: (CHAR_MAX < UCHAR_MAX),
char volatile _Atomic: (CHAR_MAX < UCHAR_MAX),
char const volatile _Atomic: (CHAR_MAX < UCHAR_MAX),

_Bool: 0,
_Bool const: 0,
_Bool volatile: 0,
_Bool const volatile: 0,
_Bool _Atomic: 0,
_Bool const _Atomic: 0,
_Bool volatile _Atomic: 0,
_Bool const volatile _Atomic: 0,

signed char: 1,
signed short: 1,
signed char const: 1,
signed short const: 1,
signed char volatile: 1,
signed short volatile: 1,
signed char const volatile: 1,
signed short const volatile: 1,
signed char _Atomic: 1,
signed short _Atomic: 1,
signed char const _Atomic: 1,
signed short const _Atomic: 1,
signed char volatile _Atomic: 1,
signed short volatile _Atomic: 1,
signed char const volatile _Atomic: 1,
signed short const volatile _Atomic: 1,

unsigned char: 0,
unsigned short: 0,
unsigned char const: 0,
unsigned short const: 0,
unsigned char volatile: 0,
unsigned short volatile: 0,
unsigned char const volatile: 0,
unsigned short const volatile: 0,
unsigned char _Atomic: 0,
unsigned short _Atomic: 0,
unsigned char const _Atomic: 0,
unsigned short const _Atomic: 0,
unsigned char volatile _Atomic: 0,
unsigned short volatile _Atomic: 0,
unsigned char const volatile _Atomic: 0,
unsigned short const volatile _Atomic: 0
)


- the case for "char" works since "unsigned char" can't have padding
bits
- _Bool is unsigned per definition
- then for all cases you have to distinguish all qualified types. at
least as quickly I didn't come up with an idea that would force an
conversion to an rvalue such that it would drop all qualifiers, and
wouldn't promote to the rank of "int"

Jens
 
Reply With Quote
 
Noob
Guest
Posts: n/a
 
      06-15-2012
James Kuyper wrote:

> John Reye wrote:
>
>> Get Jens Gustedt's fantastic P99_SIGNED macro here:
>> http://p99.gforge.inria.fr/p99-html/...eb39ccac28ebd8...

>
> That link doesn't work for me.


Google "helpfully" truncates URLs. Neat, uh?
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      06-15-2012
On 06/16/12 02:37 AM, Jens Gustedt wrote:
> Am 15.06.2012 10:51, schrieb John Reye:
>> is it possible, to determine the 'type' of a variable, if only its
>> name is given.

>
> [snip things about oldish C99]
>
>> Is there any other way, to determine if a variable var is signed or
>> unsigned, if sizeof(var)< sizeof(int) ???

>
> with C11's _Generic you may do that, provided your implementation
> hasn't extended integer types that are narrower than int,
>
> you can do something horrible like
>

<snip something horrible>
>
>
> - the case for "char" works since "unsigned char" can't have padding
> bits
> - _Bool is unsigned per definition
> - then for all cases you have to distinguish all qualified types. at
> least as quickly I didn't come up with an idea that would force an
> conversion to an rvalue such that it would drop all qualifiers, and
> wouldn't promote to the rank of "int"


This could go in a header somewhere: it's similar to C++'s use of the
numeric_limits template.

--
Ian Collins
 
Reply With Quote
 
Jens Gustedt
Guest
Posts: n/a
 
      06-16-2012
Am 15.06.2012 23:27, schrieb Ian Collins:
> On 06/16/12 02:37 AM, Jens Gustedt wrote:
>> Am 15.06.2012 10:51, schrieb John Reye:
>> with C11's _Generic you may do that, provided your implementation
>> hasn't extended integer types that are narrower than int,
>>
>> you can do something horrible like
>>

> <snip something horrible>


>
> This could go in a header somewhere:


sure it should, I think nobody ever considered using _Generic directly
in user code. The committee didn't even bother to provide a standard
"renaming" macro for it, like "bool" for "_Bool" or "alignas" for
"_Alignas".

> it's similar to C++'s use of the numeric_limits template.


generally speaken, _Generic in many places can be used where in C++
there would be a template, yes. And now that we have it in the
language effectively one could think of phasing out all these
individual MIN/MAX macros by just one for each purpose. I'll work on
that.

Jens

 
Reply With Quote
 
Tim Rentsch
Guest
Posts: n/a
 
      06-23-2012
John Reye <(E-Mail Removed)> writes:

> Ahhh!
>
> Here's a rewrite of the previous post, with BETTER FORMATTING:
>
>
>
>
> 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) !!


Two problems with sizeof: one, the size of a type is not
guaranteed to correlate with the range of a type; and two,
sizeof doesn't work on bitfield variables.

> b) ... is - as far as I know - only easy to determine,
> if sizeof(var)>= sizeof(int)


Only if the integer conversion rank of (var) is at least as
big as the integer conversion rank of (int), which is not
the same thing.

> 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/...eb39ccac28ebd8...
>
> 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"


I believe it is not.
 
Reply With Quote
 
Tim Rentsch
Guest
Posts: n/a
 
      06-23-2012
Jens Gustedt <(E-Mail Removed)> writes:

> Am 15.06.2012 10:51, schrieb John Reye:
>> is it possible, to determine the 'type' of a variable, if only its
>> name is given.

>
> [snip things about oldish C99]
>
>> Is there any other way, to determine if a variable var is signed or
>> unsigned, if sizeof(var) < sizeof(int) ???

>
> with C11's _Generic you may do that, provided your implementation
> hasn't extended integer types that are narrower than int,
>
> you can do something horrible like
>
> _Generic(var,
> default: use_the_previous_method(var),
>
> char: (CHAR_MAX < UCHAR_MAX),
> char const: (CHAR_MAX < UCHAR_MAX),
> [snip remainder]


In most cases I expect this approach will work fine, but unfortunately
it doesn't always, specifically for bitfields.

Btw, the many redundant parentheses are, well, redundant.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 14 04-03-2010 10:08 AM
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 0 04-01-2010 10:25 PM
Its a bird, its a plane, no ummm, its a Ruide thunk Ruby 1 03-30-2010 11:10 AM
Determining possible encodings of a given text Nordl÷w C Programming 3 05-06-2008 11:14 AM
Determining method type given its string name presentation and its corresponding object reference. Apple Python 3 08-01-2005 03:16 AM



Advertisments