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


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

is there a way to ask a variable what it is?

Started byT <T@invalid.invalid>
First post2020-01-28 19:19 -0800
Last post2020-02-19 12:23 +0100
Articles 20 on this page of 54 — 21 participants

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


Contents

  is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-28 19:19 -0800
    Re: is there a way to ask a variable what it is? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-29 03:47 +0000
      Re: is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-28 20:20 -0800
        Re: is there a way to ask a variable what it is? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-29 19:18 +0000
          Re: is there a way to ask a variable what it is? Bart <bc@freeuk.com> - 2020-01-29 20:58 +0000
            Re: is there a way to ask a variable what it is? nobody@example.org (Scott) - 2020-01-30 15:59 +0000
              Re: is there a way to ask a variable what it is? scott@slp53.sl.home (Scott Lurndal) - 2020-01-30 16:35 +0000
      Re: is there a way to ask a variable what it is? Siri Cruise <chine.bleu@yahoo.com> - 2020-01-28 21:55 -0800
        Re: is there a way to ask a variable what it is? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-30 00:24 +0000
          solved: Re: is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-29 21:51 -0800
            Re: solved: Re: is there a way to ask a variable what it is? fir <profesor.fir@gmail.com> - 2020-01-30 03:16 -0800
    Re: is there a way to ask a variable what it is? Ian Collins <ian-news@hotmail.com> - 2020-01-29 17:28 +1300
      Re: is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-28 20:55 -0800
        Re: is there a way to ask a variable what it is? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-01-29 06:49 +0000
        Re: is there a way to ask a variable what it is? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-29 08:08 +0100
        Re: is there a way to ask a variable what it is? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-30 00:14 +0000
          solved: Re: is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-29 21:51 -0800
          Re: is there a way to ask a variable what it is? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-02-18 08:56 -0800
            Re: is there a way to ask a variable what it is? already5chosen@yahoo.com - 2020-02-19 01:19 -0800
              Re: is there a way to ask a variable what it is? chad <cdalten@gmail.com> - 2020-02-19 02:53 -0800
              Re: is there a way to ask a variable what it is? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-02-19 12:45 +0000
              Re: is there a way to ask a variable what it is? James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-02-19 07:55 -0500
              Re: is there a way to ask a variable what it is? Bart <bc@freeuk.com> - 2020-02-19 13:02 +0000
                Re: is there a way to ask a variable what it is? chad <cdalten@gmail.com> - 2020-02-19 07:27 -0800
              Re: is there a way to ask a variable what it is? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-02-22 09:42 -0800
    Re: is there a way to ask a variable what it is? Robert Wessel <robertwessel2@yahoo.com> - 2020-01-28 22:36 -0600
      Re: is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-28 20:55 -0800
        Re: is there a way to ask a variable what it is? Manfred <noname@add.invalid> - 2020-01-29 13:22 +0100
          Re: is there a way to ask a variable what it is? David Brown <david.brown@hesbynett.no> - 2020-01-29 13:57 +0100
            Re: is there a way to ask a variable what it is? Manfred <noname@add.invalid> - 2020-01-29 17:47 +0100
              Re: is there a way to ask a variable what it is? David Brown <david.brown@hesbynett.no> - 2020-01-30 12:06 +0100
      Re: is there a way to ask a variable what it is? scott@slp53.sl.home (Scott Lurndal) - 2020-01-29 15:10 +0000
        Re: is there a way to ask a variable what it is? Manfred <noname@add.invalid> - 2020-01-29 17:07 +0100
    Re: is there a way to ask a variable what it is? Siri Cruise <chine.bleu@yahoo.com> - 2020-01-28 21:52 -0800
    Re: is there a way to ask a variable what it is? Bart <bc@freeuk.com> - 2020-01-29 11:11 +0000
      Re: is there a way to ask a variable what it is? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-29 13:30 +0100
        Re: is there a way to ask a variable what it is? Bart <bc@freeuk.com> - 2020-01-29 12:44 +0000
          Re: is there a way to ask a variable what it is? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-29 14:03 +0100
            Re: is there a way to ask a variable what it is? gazelle@shell.xmission.com (Kenny McCormack) - 2020-01-29 14:03 +0000
            Re: is there a way to ask a variable what it is? Bart <bc@freeuk.com> - 2020-01-29 14:30 +0000
              Re: is there a way to ask a variable what it is? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-29 15:45 +0100
                Re: is there a way to ask a variable what it is? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-29 16:28 +0100
                  Re: is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-29 08:35 -0800
                  Re: is there a way to ask a variable what it is? Bart <bc@freeuk.com> - 2020-01-29 17:34 +0000
                    Re: is there a way to ask a variable what it is? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-29 18:48 +0100
      Re: is there a way to ask a variable what it is? Thiago Adams <thiago.adams@gmail.com> - 2020-01-29 04:43 -0800
        Re: is there a way to ask a variable what it is? David Brown <david.brown@hesbynett.no> - 2020-01-29 14:46 +0100
          Re: is there a way to ask a variable what it is? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-01-29 15:15 +0000
            Re: is there a way to ask a variable what it is? fir <profesor.fir@gmail.com> - 2020-01-29 08:11 -0800
            Re: is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-29 08:38 -0800
    Re: is there a way to ask a variable what it is? Bart <bc@freeuk.com> - 2020-01-29 11:23 +0000
    Re: is there a way to ask a variable what it is? John Bode <jfbode1029@gmail.com> - 2020-01-29 11:55 -0800
    Re: is there a way to ask a variable what it is? jacobnavia <jacob@jacob.remcomp.fr> - 2020-02-19 12:23 +0100
    Re: is there a way to ask a variable what it is? jacobnavia <jacob@jacob.remcomp.fr> - 2020-02-19 12:23 +0100

Page 1 of 3  [1] 2 3  Next page →


#149463 — is there a way to ask a variable what it is?

FromT <T@invalid.invalid>
Date2020-01-28 19:19 -0800
Subjectis there a way to ask a variable what it is?
Message-ID<r0qtkb$3qj$1@dont-email.me>
Hi All,

Trivia question:

In C, after you declare a variable, is there a way
to turn around and ask it what that variable is?

Some thing like Raku's

     > my uint $u; say $u.^name
     Int

only correct?

Many thanks,
-T

[toc] | [next] | [standalone]


#149464

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2020-01-29 03:47 +0000
Message-ID<87lfpqrkit.fsf@bsb.me.uk>
In reply to#149463
T <T@invalid.invalid> writes:

> Trivia question:
>
> In C, after you declare a variable, is there a way
> to turn around and ask it what that variable is?

You seem to mean what type it is.  No.  There is no way to find that
out.

This prompts a follow-up question.  What would you want to do with it?
It's possible that what you want to achieve could be done in some other
way. 

> Some thing like Raku's
>
>     > my uint $u; say $u.^name
>     Int
>
> only correct?

I don't think that's wrong, but it's not topical here.

-- 
Ben.

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


#149465

FromT <T@invalid.invalid>
Date2020-01-28 20:20 -0800
Message-ID<r0r17d$jbq$1@dont-email.me>
In reply to#149464
On 2020-01-28 19:47, Ben Bacarisse wrote:
> T <T@invalid.invalid> writes:
> 
>> Trivia question:
>>
>> In C, after you declare a variable, is there a way
>> to turn around and ask it what that variable is?
> 
> You seem to mean what type it is.  No.  There is no way to find that
> out.
> 
> This prompts a follow-up question.  What would you want to do with it?
> It's possible that what you want to achieve could be done in some other
> way.

In Raku, I can define a variable as a subset, such
as a string and an integer.  I ask the variable what
it is to determine what details are inside it


> 
>> Some thing like Raku's
>>
>>      > my uint $u; say $u.^name
>>      Int
>>
>> only correct?
> 
> I don't think that's wrong, but it's not topical here.


Techincally, it is not wrong.  This is like Quantum
Observation where observing the thing changes it.

^name changes the variable from unit (unsigned integer)
to UInt (magical unsigned integer that can have as
many byte in it as your heart desires).  Only
You have no clue the variable got changed.  But
the result is always consistent.

Get a load of this!

 > my UInt $x = 0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF;

95780971304118053647396689196894323976171195136475135

The reason for the trivia question  is that Raku
is written in C and if you ask too technical a question,
you get C quoted at you.

-T

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


#149506

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2020-01-29 19:18 +0000
Message-ID<87blqmqdek.fsf@bsb.me.uk>
In reply to#149465
T <T@invalid.invalid> writes:

> On 2020-01-28 19:47, Ben Bacarisse wrote:
>> T <T@invalid.invalid> writes:
>>
>>> Trivia question:
>>>
>>> In C, after you declare a variable, is there a way
>>> to turn around and ask it what that variable is?
>>
>> You seem to mean what type it is.  No.  There is no way to find that
>> out.
>>
>> This prompts a follow-up question.  What would you want to do with it?
>> It's possible that what you want to achieve could be done in some other
>> way.
>
> In Raku, I can define a variable as a subset, such
> as a string and an integer.  I ask the variable what
> it is to determine what details are inside it

That was not really my point.  If you have a C problem, this is the
place to ask, but the question has to be about programming goals, not
how to I, in C, all the things I do in Raku.

Let me illustrate.  If knowing the type is the top-level goal, the
answer is simple: you can't.  But if you want to kow the type in order
to find out its range, you may be able to find suitable alternatives in
C.

<cut Raku stuff>
-- 
Ben.

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


#149510

FromBart <bc@freeuk.com>
Date2020-01-29 20:58 +0000
Message-ID<emmYF.251862$_R5.228244@fx19.am4>
In reply to#149506
On 29/01/2020 20:49, Stefan Ram wrote:
> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>> Let me illustrate.  If knowing the type is the top-level goal, the
>> answer is simple: you can't.
> 
>    Knowing the type of a variable?
> 
>    It can be read from the declaration.
> 
>    For example,
> 
> double c;
> 
>    The type of »c« is double.


That's a simple example. Try:

    clock_t c;

What's is the actual type of c? Or a little more challenging:

    #ifdef __GNUC__
        clock_t c;
    #elif MAC1           // set via -D compiler option
        float c;
    #elif __LINE__ == 17
        char* c;
    #else
        long long int c;
    #endif


Or you want to know whether pointers to A and B will be compatible:

    int64_t A;
    long long B;

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


#149532

Fromnobody@example.org (Scott)
Date2020-01-30 15:59 +0000
Message-ID<5e32fa92.4262077640@core>
In reply to#149510
On Wed, 29 Jan 2020 20:58:47 +0000, Bart <bc@freeuk.com> wrote:

>On 29/01/2020 20:49, Stefan Ram wrote:
>> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>>> Let me illustrate.  If knowing the type is the top-level goal, the
>>> answer is simple: you can't.
>> 
>>    Knowing the type of a variable?
>> 
>>    It can be read from the declaration.
>> 
>>    For example,
>> 
>> double c;
>> 
>>    The type of »c« is double.
>
>
>That's a simple example. Try:
>
>    clock_t c;
>
>What's is the actual type of c? Or a little more challenging:

I have one (silly) answer.

$cat tmp.c
#include <stdio.h>
int main(void) {
	clock_t x;
	printf("%c\n", x);
}

$gcc -Wformat tmp.c
tmp.c: In function 'main':
tmp.c:4:18: warning: format '%c' expects argument of type 'int', but
argument 2 has type 'clock_t {aka long unsigned int}' [-Wformat=]
         printf("%c\n", x);
                 ~^
                 %ld


There, clock_t is long unsigned. I don't have any idea how to do this
at runtime, or with any other compiler.

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


#149533

Fromscott@slp53.sl.home (Scott Lurndal)
Date2020-01-30 16:35 +0000
Message-ID<HBDYF.30903$Ys3.23851@fx21.iad>
In reply to#149532
nobody@example.org (Scott) writes:
>On Wed, 29 Jan 2020 20:58:47 +0000, Bart <bc@freeuk.com> wrote:
>
>>On 29/01/2020 20:49, Stefan Ram wrote:
>>> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>>>> Let me illustrate.  If knowing the type is the top-level goal, the
>>>> answer is simple: you can't.
>>> 
>>>    Knowing the type of a variable?
>>> 
>>>    It can be read from the declaration.
>>> 
>>>    For example,
>>> 
>>> double c;
>>> 
>>>    The type of »c« is double.
>>
>>
>>That's a simple example. Try:
>>
>>    clock_t c;
>>
>>What's is the actual type of c? Or a little more challenging:
>
>I have one (silly) answer.
>
>$cat tmp.c
>#include <stdio.h>
>int main(void) {
>	clock_t x;
>	printf("%c\n", x);
>}
>
>$gcc -Wformat tmp.c
>tmp.c: In function 'main':
>tmp.c:4:18: warning: format '%c' expects argument of type 'int', but
>argument 2 has type 'clock_t {aka long unsigned int}' [-Wformat=]
>         printf("%c\n", x);
>                 ~^
>                 %ld
>
>
>There, clock_t is long unsigned. I don't have any idea how to do this
>at runtime, or with any other compiler.
>

There's always the source:

   The <sys/types.h> header shall define at least the following types:

   blkcnt_t
      Used for file block counts.
   blksize_t
      Used for block sizes.
   clock_t
      Used for system times in clock ticks or CLOCKS_PER_SEC; see <time.h>.

...

   Additionally:
     * clock_t shall be an integer or real-floating type.

https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_types.h.html

Albeit I've never seen it implemented as a real-floating type.

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


#149471

FromSiri Cruise <chine.bleu@yahoo.com>
Date2020-01-28 21:55 -0800
Message-ID<chine.bleu-AE8637.21552528012020@reader.eternal-september.org>
In reply to#149464
In article <87lfpqrkit.fsf@bsb.me.uk>,
 Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:

> This prompts a follow-up question.  What would you want to do with it?
> It's possible that what you want to achieve could be done in some other
> way. 

Implement a fprintf like macro in m4. Rather than hope you got 
the right basic type (int, double, etc) and the right length 
(long, long long, short), the macro can encode the parameters 
types into the code. The implementation will know exactly what's 
on the var-args.

-- 
:-<> Siri Seal of Disavowal #000-001. Disavowed. Denied. Deleted.    @
'I desire mercy, not sacrifice.'                                    /|\
The first law of discordiamism: The more energy   This post         / \
to make order is nore energy made into entropy.   insults Islam.  Mohammed

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


#149515

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2020-01-30 00:24 +0000
Message-ID<87tv4dpz9q.fsf@bsb.me.uk>
In reply to#149471
Siri Cruise <chine.bleu@yahoo.com> writes:

> In article <87lfpqrkit.fsf@bsb.me.uk>,
>  Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
>
>> This prompts a follow-up question.  What would you want to do with it?
>> It's possible that what you want to achieve could be done in some other
>> way. 
>
> Implement a fprintf like macro in m4. Rather than hope you got 
> the right basic type (int, double, etc) and the right length 
> (long, long long, short), the macro can encode the parameters 
> types into the code. The implementation will know exactly what's 
> on the var-args.

Yes.  My "you" was literal and directed at the OP.  I (and you and
others) can come up with all sorts of examples where this information
could be useful, but I wanted to move the OP onto something we might be
able to help with by offering alternative techniques rather than simply
saying "computer says no".

(I say "yes" but I did not really understand your example.  Just a
type-checked version of printf would be a simple use for run-time type
information.)

-- 
Ben.

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


#149516 — solved: Re: is there a way to ask a variable what it is?

FromT <T@invalid.invalid>
Date2020-01-29 21:51 -0800
Subjectsolved: Re: is there a way to ask a variable what it is?
Message-ID<r0tqs7$kuf$1@dont-email.me>
In reply to#149515
On 2020-01-29 16:24, Ben Bacarisse wrote:
> Siri Cruise <chine.bleu@yahoo.com> writes:
> 
>> In article <87lfpqrkit.fsf@bsb.me.uk>,
>>   Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
>>
>>> This prompts a follow-up question.  What would you want to do with it?
>>> It's possible that what you want to achieve could be done in some other
>>> way.
>>
>> Implement a fprintf like macro in m4. Rather than hope you got
>> the right basic type (int, double, etc) and the right length
>> (long, long long, short), the macro can encode the parameters
>> types into the code. The implementation will know exactly what's
>> on the var-args.
> 
> Yes.  My "you" was literal and directed at the OP.  I (and you and
> others) can come up with all sorts of examples where this information
> could be useful, but I wanted to move the OP onto something we might be
> able to help with by offering alternative techniques rather than simply
> saying "computer says no".
> 
> (I say "yes" but I did not really understand your example.  Just a
> type-checked version of printf would be a simple use for run-time type
> information.)
> 

Gee wiz, it was just a trivia question.  The answer was"no".
That was all I was interest in.

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


#149521 — Re: solved: Re: is there a way to ask a variable what it is?

Fromfir <profesor.fir@gmail.com>
Date2020-01-30 03:16 -0800
SubjectRe: solved: Re: is there a way to ask a variable what it is?
Message-ID<1cea6c91-03b4-469a-a0ef-695629ddf043@googlegroups.com>
In reply to#149516
W dniu czwartek, 30 stycznia 2020 06:51:12 UTC+1 użytkownik T napisał:
> On 2020-01-29 16:24, Ben Bacarisse wrote:
> > Siri Cruise <chine.bleu@yahoo.com> writes:
> > 
> >> In article <87lfpqrkit.fsf@bsb.me.uk>,
> >>   Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
> >>
> >>> This prompts a follow-up question.  What would you want to do with it?
> >>> It's possible that what you want to achieve could be done in some other
> >>> way.
> >>
> >> Implement a fprintf like macro in m4. Rather than hope you got
> >> the right basic type (int, double, etc) and the right length
> >> (long, long long, short), the macro can encode the parameters
> >> types into the code. The implementation will know exactly what's
> >> on the var-args.
> > 
> > Yes.  My "you" was literal and directed at the OP.  I (and you and
> > others) can come up with all sorts of examples where this information
> > could be useful, but I wanted to move the OP onto something we might be
> > able to help with by offering alternative techniques rather than simply
> > saying "computer says no".
> > 
> > (I say "yes" but I did not really understand your example.  Just a
> > type-checked version of printf would be a simple use for run-time type
> > information.)
> > 
> 
> Gee wiz, it was just a trivia question.  The answer was"no".
> That was all I was interest in.

this is discusion goups meant to be be sometimes suitable for discussing more indepth problems

if you want such trivial answers more suitable place is irc (here is some chanell for c on freenode?) you will get that answers in a pack in a seconds via irc 

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


#149466

FromIan Collins <ian-news@hotmail.com>
Date2020-01-29 17:28 +1300
Message-ID<h9cfvbF25s3U14@mid.individual.net>
In reply to#149463
On 29/01/2020 16:19, T wrote:
> Hi All,
> 
> Trivia question:
> 
> In C, after you declare a variable, is there a way
> to turn around and ask it what that variable is?

You are looking for reflection, which C lacks.

-- 
Ian.

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


#149468

FromT <T@invalid.invalid>
Date2020-01-28 20:55 -0800
Message-ID<r0r37k$r04$1@dont-email.me>
In reply to#149466
On 2020-01-28 20:28, Ian Collins wrote:
> On 29/01/2020 16:19, T wrote:
>> Hi All,
>>
>> Trivia question:
>>
>> In C, after you declare a variable, is there a way
>> to turn around and ask it what that variable is?
> 
> You are looking for reflection, which C lacks.
> 

Not sure what a reflection.

I really don't think C can do this without have a
structure assigned to every variable.

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


#149472

FromJorgen Grahn <grahn+nntp@snipabacken.se>
Date2020-01-29 06:49 +0000
Message-ID<slrnr32ao5.km3.grahn+nntp@frailea.sa.invalid>
In reply to#149468
On Wed, 2020-01-29, T wrote:
> On 2020-01-28 20:28, Ian Collins wrote:
>> On 29/01/2020 16:19, T wrote:
>>> Hi All,
>>>
>>> Trivia question:
>>>
>>> In C, after you declare a variable, is there a way
>>> to turn around and ask it what that variable is?
>> 
>> You are looking for reflection, which C lacks.
>
> Not sure what a reflection.

He simply gave you the technical name for the feature you asked for.
So you can e.g. read about it if you want to.

/Jorgen

-- 
  // Jorgen Grahn <grahn@  Oo  o.   .     .
\X/     snipabacken.se>   O  o   .

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


#149473

FromBonita Montero <Bonita.Montero@gmail.com>
Date2020-01-29 08:08 +0100
Message-ID<r0rb11$qh6$1@news.albasani.net>
In reply to#149468
>>> In C, after you declare a variable, is there a way
>>> to turn around and ask it what that variable is?

>> You are looking for reflection, which C lacks.

> Not sure what a reflection.

You're asking for a feature and don't know what it's good for.

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


#149514

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2020-01-30 00:14 +0000
Message-ID<87zhe5pzqf.fsf@bsb.me.uk>
In reply to#149468
T <T@invalid.invalid> writes:

> On 2020-01-28 20:28, Ian Collins wrote:
>> On 29/01/2020 16:19, T wrote:
>>> Hi All,
>>>
>>> Trivia question:
>>>
>>> In C, after you declare a variable, is there a way
>>> to turn around and ask it what that variable is?
>>
>> You are looking for reflection, which C lacks.
>
> Not sure what a reflection.

Reflection in programming languages are those features that allow
programs to talk about programs (or, more often, parts of programs).  C
has almost no features that fit that definition.

> I really don't think C can do this without have a
> structure assigned to every variable.

Your "this" is vague, but C is a statically typed language so all type
information, and everything easily derived from type information, is
known at compile time.

-- 
Ben.

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


#149517 — solved: Re: is there a way to ask a variable what it is?

FromT <T@invalid.invalid>
Date2020-01-29 21:51 -0800
Subjectsolved: Re: is there a way to ask a variable what it is?
Message-ID<r0tqt4$kuf$2@dont-email.me>
In reply to#149514
On 2020-01-29 16:14, Ben Bacarisse wrote:
> Your "this" is vague, but C is a statically typed language so all type
> information, and everything easily derived from type information, is
> known at compile time.

Perfect.

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


#150074

FromTim Rentsch <tr.17687@z991.linuxsc.com>
Date2020-02-18 08:56 -0800
Message-ID<86lfozeswm.fsf@linuxsc.com>
In reply to#149514
Ben Bacarisse <ben.usenet@bsb.me.uk> writes:

> [...] C is a statically typed language so all type information,
> and everything easily derived from type information, is known at
> compile time.

I don't think that's right exactly.  For one example, some type
information may be known in one translation unit but not known
in another.  A declaration like

    extern int special_values[];

offers partial but not complete type information in this TU, and so
is not known at compile time.  A second example is variable length
arrays.  Here the type information is not known until run time, and
usually is not fixed but varies with different invocations.  A third
example pertains to structures and unions in dynamically allocated
memory, where code inspects a tag using the "one special guarantee"
for structure member access, to select a specific struct type (using
a cast) at run time.  Certainly there are other examples but I think
these provide enough variety and flavor to motivate the objection.

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


#150076

Fromalready5chosen@yahoo.com
Date2020-02-19 01:19 -0800
Message-ID<75f1fb82-5e5d-40d5-88e6-a701c17d619f@googlegroups.com>
In reply to#150074
On Tuesday, February 18, 2020 at 6:56:18 PM UTC+2, Tim Rentsch wrote:
> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
> 
> > [...] C is a statically typed language so all type information,
> > and everything easily derived from type information, is known at
> > compile time.
> 
> I don't think that's right exactly.  For one example, some type
> information may be known in one translation unit but not known
> in another.  A declaration like
> 
>     extern int special_values[];
> 
> offers partial but not complete type information in this TU, and so
> is not known at compile time.

What is "a real type", if there is such thing, is unknown, but what compiler think about the type when it deals with this specific translation unit is know.

> A second example is variable length
> arrays.  Here the type information is not known until run time, and
> usually is not fixed but varies with different invocations. 

Here we are at philosophical question of "what is considered a type?".
You appear to come from school of original Pascal, where length of array is considered part of its type. That's not how traditional 'C' sees it. 
Anyway, VLA are already optional in the latest Standard and hopefully will be marked deprecated in the next one. I am of opinion that their addition in c99 was a mistake.

> A third
> example pertains to structures and unions in dynamically allocated
> memory, where code inspects a tag using the "one special guarantee"
> for structure member access, to select a specific struct type (using
> a cast) at run time.  

That is similar to the first case: the "real" type is unknown, but the type, assumed by compiler at any given point/statement is known.

> Certainly there are other examples but I think
> these provide enough variety and flavor to motivate the objection.

Only VLA case and only if we use the word "type" is unusual [for 'c' and for majority of other algolish languages, including majority of real-world Pascal dialects] sense.

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


#150077

Fromchad <cdalten@gmail.com>
Date2020-02-19 02:53 -0800
Message-ID<a67a767e-5d78-46a5-8c66-262f050223bf@googlegroups.com>
In reply to#150076
I've seen some textbooks use the terms "apparent type" and "actual type".

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


Page 1 of 3  [1] 2 3  Next page →

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


csiph-web