Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #22280 > unrolled thread

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

Started byJohn Reye <jononanon@googlemail.com>
First post2012-06-15 01:47 -0700
Last post2012-06-22 20:31 -0700
Articles 14 — 9 participants

Back to article view | Back to comp.lang.c


Contents

  Determining the 'type' of a variable, if only its name is given John Reye <jononanon@googlemail.com> - 2012-06-15 01:47 -0700
    Re: Determining the 'type' of a variable, if only its name is given John Reye <jononanon@googlemail.com> - 2012-06-15 01:51 -0700
      Re: Determining the 'type' of a variable, if only its name is given James Kuyper <jameskuyper@verizon.net> - 2012-06-15 08:18 -0400
        Re: Determining the 'type' of a variable, if only its name is given "BartC" <bc@freeuk.com> - 2012-06-15 13:43 +0100
        Re: Determining the 'type' of a variable, if only its name is given Noob <root@127.0.0.1> - 2012-06-15 17:02 +0200
          Re: Determining the 'type' of a variable, if only its name is given Robert Miles <milesrf@Usenet-News.net> - 2012-07-23 23:20 -0500
      Re: Determining the 'type' of a variable, if only its name is given pete <pfiland@mindspring.com> - 2012-06-15 08:21 -0400
      Re: Determining the 'type' of a variable, if only its name is given Jens Gustedt <jens.gustedt@loria.fr> - 2012-06-15 16:37 +0200
        Re: Determining the 'type' of a variable, if only its name is given Ian Collins <ian-news@hotmail.com> - 2012-06-16 09:27 +1200
          Re: Determining the 'type' of a variable, if only its name is given Jens Gustedt <jens.gustedt@loria.fr> - 2012-06-16 09:38 +0200
        Re: Determining the 'type' of a variable, if only its name is given Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-22 20:42 -0700
          Re: Determining the 'type' of a variable, if only its name is given Jens Gustedt <jens.gustedt@loria.fr> - 2012-06-23 14:41 +0200
            Re: Determining the 'type' of a variable, if only its name is given Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-24 10:15 -0700
      Re: Determining the 'type' of a variable, if only its name is given Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-22 20:31 -0700

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

FromJohn Reye <jononanon@googlemail.com>
Date2012-06-15 01:47 -0700
SubjectDetermining the 'type' of a variable, if only its name is given
Message-ID<caa6501b-646a-4ca3-85a2-a15bf7271d90@h41g2000yqm.googlegroups.com>
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/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"

[toc] | [next] | [standalone]


#22281

FromJohn Reye <jononanon@googlemail.com>
Date2012-06-15 01:51 -0700
Message-ID<966f3de9-bf1f-418a-b3e0-fbeecfa241a5@j10g2000yqd.googlegroups.com>
In reply to#22280
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/group__integers_ga3eb39ccac28ebd8...

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"

[toc] | [prev] | [next] | [standalone]


#22289

FromJames Kuyper <jameskuyper@verizon.net>
Date2012-06-15 08:18 -0400
Message-ID<jrf95p$2dt$1@dont-email.me>
In reply to#22281
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/group__integers_ga3eb39ccac28ebd8...

That link doesn't work for me.
-- 
James Kuyper

[toc] | [prev] | [next] | [standalone]


#22294

From"BartC" <bc@freeuk.com>
Date2012-06-15 13:43 +0100
Message-ID<jrfald$b3k$1@dont-email.me>
In reply to#22289

"James Kuyper" <jameskuyper@verizon.net> wrote in message 
news:jrf95p$2dt$1@dont-email.me...
> 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/group__integers_ga3eb39ccac28ebd8...
>
> That link doesn't work for me.

Try the one in the original post.

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

-- 
bartc


 

[toc] | [prev] | [next] | [standalone]


#22297

FromNoob <root@127.0.0.1>
Date2012-06-15 17:02 +0200
Message-ID<jrfiqd$sui$1@dont-email.me>
In reply to#22289
James Kuyper wrote:

> John Reye wrote:
> 
>> Get Jens Gustedt's fantastic P99_SIGNED macro here:
>> http://p99.gforge.inria.fr/p99-html/group__integers_ga3eb39ccac28ebd8...
> 
> That link doesn't work for me.

Google "helpfully" truncates URLs. Neat, uh?

[toc] | [prev] | [next] | [standalone]


#23649

FromRobert Miles <milesrf@Usenet-News.net>
Date2012-07-23 23:20 -0500
Message-ID<500e214d$0$8813$882e7ee2@usenet-news.net>
In reply to#22297
On 6/15/2012 10:02 AM, Noob wrote:
> James Kuyper wrote:
>
>> John Reye wrote:
>>
>>> Get Jens Gustedt's fantastic P99_SIGNED macro here:
>>> http://p99.gforge.inria.fr/p99-html/group__integers_ga3eb39ccac28ebd8...
>>
>> That link doesn't work for me.
>
> Google "helpfully" truncates URLs. Neat, uh?

Somewhat helpful for Google Groups users.  Not helpful
for the rest of Usenet, since they don't convert their
own messages containing URLs to NNTP format correctly
for long URLs.

[toc] | [prev] | [next] | [standalone]


#22290

Frompete <pfiland@mindspring.com>
Date2012-06-15 08:21 -0400
Message-ID<4FDB28E2.2D79@mindspring.com>
In reply to#22281
John Reye wrote:
 

> http://p99.gforge.inria.fr/p99-html/group__integers_ga3eb39ccac28ebd8...

It works better with my webrowser, 
if instead I use this URL:

http://p99.gforge.inria.fr/p99-html/group__integers.html

-- 
pete

[toc] | [prev] | [next] | [standalone]


#22296

FromJens Gustedt <jens.gustedt@loria.fr>
Date2012-06-15 16:37 +0200
Message-ID<4FDB48C6.6030802@loria.fr>
In reply to#22281
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

[toc] | [prev] | [next] | [standalone]


#22305

FromIan Collins <ian-news@hotmail.com>
Date2012-06-16 09:27 +1200
Message-ID<a41nlfFg0U1@mid.individual.net>
In reply to#22296
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

[toc] | [prev] | [next] | [standalone]


#22312

FromJens Gustedt <jens.gustedt@loria.fr>
Date2012-06-16 09:38 +0200
Message-ID<4FDC380B.6060805@loria.fr>
In reply to#22305
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

[toc] | [prev] | [next] | [standalone]


#22571

FromTim Rentsch <txr@alumni.caltech.edu>
Date2012-06-22 20:42 -0700
Message-ID<kfntxy2u215.fsf@x-alumni2.alumni.caltech.edu>
In reply to#22296
Jens Gustedt <jens.gustedt@loria.fr> 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.

[toc] | [prev] | [next] | [standalone]


#22580

FromJens Gustedt <jens.gustedt@loria.fr>
Date2012-06-23 14:41 +0200
Message-ID<4FE5B982.3080100@loria.fr>
In reply to#22571
Hi

Am 23.06.2012 05:42, schrieb Tim Rentsch:
> Jens Gustedt <jens.gustedt@loria.fr> writes:
>
>> 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,

It did some more experiments with this and actually found that the
handling of qualified types is not very well designed with this new
_Generic tool. In the case it was asked here (for a variable, i.e an
lvalue) it seems consistent, but for rvalues it is completely
underspecified.

I'll post a question comp.std.c about this

> specifically for bitfields.

the signedness of bitfields is really a special case oddity. What
should _Generic trigger, here. The declared type or the effective
type?

A bitfield that is declared with just "int" could (implementation
defined) well be unsigned in nature. To the extreme on a system with
32 bit wide "int", a bit field with specification "int a:32" could be
unsigned.

> Btw, the many redundant parentheses are, well, redundant.

but not superficial

coding styles vary, as you probably know

Jens

[toc] | [prev] | [next] | [standalone]


#22612

FromTim Rentsch <txr@alumni.caltech.edu>
Date2012-06-24 10:15 -0700
Message-ID<kfn8vfctyw5.fsf@x-alumni2.alumni.caltech.edu>
In reply to#22580
Jens Gustedt <jens.gustedt@loria.fr> writes:

> Hi
>
> Am 23.06.2012 05:42, schrieb Tim Rentsch:
>> Jens Gustedt <jens.gustedt@loria.fr> writes:
>>
>>> 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,
>
> It did some more experiments with this and actually found that the
> handling of qualified types is not very well designed with this new
> _Generic tool. In the case it was asked here (for a variable, i.e an
> lvalue) it seems consistent, but for rvalues it is completely
> underspecified.
>
> I'll post a question comp.std.c about this

Okay, I'll look over at that.


>> specifically for bitfields.
>
> the signedness of bitfields is really a special case oddity. What
> should _Generic trigger, here. The declared type or the effective
> type?

Good question!  I wonder if the Standard says, or if a DR should
be filed for that?

> A bitfield that is declared with just "int" could (implementation
> defined) well be unsigned in nature. To the extreme on a system with
> 32 bit wide "int", a bit field with specification "int a:32" could be
> unsigned.

Not just could be but would be, on a system where 'int' means
unsigned int for bitfields.

My point about bitfields (which was a bit off the mark for the
particular context here, but oh well) is that using _Generic for
bitfields gives misleading results as regards their behavior
under integer promotion rules (ie, a bitfield declared as
'unsigned x:3' will show up as unsigned int but will promote to
an int).  More confusingly, a bitfield is an lvalue but cannot
have its address taken.  It would be nice if these distinctions
could be handled under _Generic, but alas they cannot.


>> Btw, the many redundant parentheses are, well, redundant.
>
> but not superficial
>
> coding styles vary, as you probably know

My comment was only about the redundancy, not about style.
However, on the issue of style, IME style rules that promote
using redundant parentheses almost always do more harm than
good.  I don't see any added value in using parentheses as
they were in the example above.  Conversely, I do see a big
negative, in that people unfamiliar with the new _Generic
construct might come away with the mistaken impression that
those parentheses are required.  The reason for my comment
was to correct any such mistaken conclusions.

[toc] | [prev] | [next] | [standalone]


#22570

FromTim Rentsch <txr@alumni.caltech.edu>
Date2012-06-22 20:31 -0700
Message-ID<kfny5neu2j8.fsf@x-alumni2.alumni.caltech.edu>
In reply to#22281
John Reye <jononanon@googlemail.com> 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/group__integers_ga3eb39ccac28ebd8...
>
> 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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web