473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

getting variable type in C

Hi,

I am pretty sure this is not possible, but maybe somehow it is.

Given a variable, can I tell what type it is at runtime?

Michael
Nov 14 '05 #1
26 2380

On Sat, 13 Nov 2004, Michael McGarry wrote:

Given a variable, can I tell what type it is at runtime?


Sure. Just take the type it was at compile time, and remember it.
For example,

int main(void)
{
int i;
printf("i is of type %s.\n", "int");
return 0;
}

If you have some other idea in mind, you're going to have to be more
explicit.

-Arthur
Nov 14 '05 #2
On Sun, 14 Nov 2004 04:28:36 UTC, Michael McGarry
<mm******@nospam.org> wrote:
Hi,

I am pretty sure this is not possible, but maybe somehow it is.

Given a variable, can I tell what type it is at runtime?


No. Your compiler replaces names with addresses. Nothing as the human
writing the souerces needs names for variables, so the compiler
eliminates them.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation

Nov 14 '05 #3

"Michael McGarry" <mm******@nospam.org> wrote in message news:T9Bld.58779$EZ.51910@okepread07...
Hi,

I am pretty sure this is not possible, but maybe somehow it is.

Given a variable, can I tell what type it is at runtime?

[snip]
In C++ :
* http://groups.google.com/groups?selm...news.dfncis.de
* http://alexvn.freeservers.com/s1/download.html (Recognition of simple variables, pointers and arrays)

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Nov 14 '05 #4
On Sat, 13 Nov 2004 21:28:36 -0700, in comp.lang.c , Michael McGarry
<mm******@nospam.org> wrote:
Hi,

I am pretty sure this is not possible, but maybe somehow it is.
Given a variable, can I tell what type it is at runtime?


You're right - there's no typeof() operator or function in C. By runtime,
all variables are replaced by addresses etc and there's no type information
carted around with them.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #5
Michael McGarry <mm******@nospam.org> wrote in message news:<T9Bld.58779$EZ.51910@okepread07>...
Hi,

I am pretty sure this is not possible, but maybe somehow it is.

Given a variable, can I tell what type it is at runtime?

Michael


By size, maybe ?...
Nov 14 '05 #6
In article <T9Bld.58779$EZ.51910@okepread07>,
Michael McGarry <mm******@nospam.org> wrote:
Hi,

I am pretty sure this is not possible, but maybe somehow it is.

Given a variable, can I tell what type it is at runtime?


Some compilers, like gcc, provide a construct named
typeof() which produces the object type. Unfortunately,
(and inexplicably) typeof() has not found its way into
the C standard.

--
Rouben Rostamian
Nov 14 '05 #7
Rouben Rostamian wrote:
In article <T9Bld.58779$EZ.51910@okepread07>,
Michael McGarry <mm******@nospam.org> wrote:
Hi,

I am pretty sure this is not possible, but maybe somehow it is.

Given a variable, can I tell what type it is at runtime?

Some compilers, like gcc, provide a construct named
typeof() which produces the object type. Unfortunately,
(and inexplicably) typeof() has not found its way into
the C standard.

Is there really a typeof()?? Which standard library is it in?

Michael
Nov 14 '05 #8
Michael McGarry wrote:
Rouben Rostamian wrote:
In article <T9Bld.58779$EZ.51910@okepread07>,
Michael McGarry <mm******@nospam.org> wrote:
Hi,

I am pretty sure this is not possible, but maybe somehow it is.

Given a variable, can I tell what type it is at runtime?


Some compilers, like gcc, provide a construct named
typeof() which produces the object type. Unfortunately,
(and inexplicably) typeof() has not found its way into
the C standard.

Is there really a typeof()?? Which standard library is it in?

Michael


No typeof in C.
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #9
On Sun, 14 Nov 2004 21:49:14 -0500, Joe Wright wrote:
Michael McGarry wrote:
Rouben Rostamian wrote:
In article <T9Bld.58779$EZ.51910@okepread07>,
Michael McGarry <mm******@nospam.org> wrote:

Hi,

I am pretty sure this is not possible, but maybe somehow it is.

Given a variable, can I tell what type it is at runtime?

Some compilers, like gcc, provide a construct named
typeof() which produces the object type. Unfortunately,
(and inexplicably) typeof() has not found its way into
the C standard.

Is there really a typeof()?? Which standard library is it in?

Michael


No typeof in C.


I don't understand the confusion/sarcasm here. Rouben clearly stated it
was not standard. It is however a useful feature available to many
developers and there is no harm voicing the fact that it exists (along
with the fact that it is not standard as Rouben did) when there is no
standard solution and the information may help the OP.

Rob Gamble
Nov 14 '05 #10
Quoth Michael McGarry on or about 2004-11-14:
Some compilers, like gcc, provide a construct named
typeof() which produces the object type.


Is there really a typeof()?? Which standard library is it in?


Typeof *IS NOT STANDARD*. Now we're clear on that, if you have the GCC,
typing
info gcc 'C Extensions' Typeof

will provide you with documentation (for the GCC version).

-trent
Nov 14 '05 #11
On Sun, 14 Nov 2004 13:32:08 +0000, Rouben Rostamian wrote:
In article <T9Bld.58779$EZ.51910@okepread07>,
Michael McGarry <mm******@nospam.org> wrote:
Hi,

I am pretty sure this is not possible, but maybe somehow it is.

Given a variable, can I tell what type it is at runtime?

How would you want this information to be represented? How would you use
it? Remember that C is statically typed. You know at the point you use
a variable what type it is, you just have to look at the definition for
the variable.
Some compilers, like gcc, provide a construct named
typeof() which produces the object type.
AFAIK this is just a band-aid for macros, replicating a type in
another declaration or a cast - strictly a compile time thing. It doesn't
address the OP's question of a runtime test. Since C doesn't support
Runtime Type Information, inheritance or any sort of dynamic typing a
runtime test doesn't make much sense - it just tells you what you already
know.
Unfortunately,
(and inexplicably) typeof() has not found its way into
the C standard.


There may be the odd occasion where it is useful but don't see that these
are common enough to make it a language requirement.

Lawrence

Nov 14 '05 #12
In article <pa***********************@netactive.co.uk>,
Lawrence Kirby <lk****@netactive.co.uk> wrote:
On Sun, 14 Nov 2004 13:32:08 +0000, Rouben Rostamian wrote:
In article <T9Bld.58779$EZ.51910@okepread07>,
Michael McGarry <mm******@nospam.org> wrote:
Hi,

I am pretty sure this is not possible, but maybe somehow it is.

Given a variable, can I tell what type it is at runtime?


How would you want this information to be represented? How would you use
it? Remember that C is statically typed. You know at the point you use
a variable what type it is, you just have to look at the definition for
the variable.
Some compilers, like gcc, provide a construct named
typeof() which produces the object type.


AFAIK this is just a band-aid for macros, replicating a type in
another declaration or a cast - strictly a compile time thing. It doesn't
address the OP's question of a runtime test. Since C doesn't support
Runtime Type Information, inheritance or any sort of dynamic typing a
runtime test doesn't make much sense - it just tells you what you already
know.
Unfortunately,
(and inexplicably) typeof() has not found its way into
the C standard.


There may be the odd occasion where it is useful but don't see that these
are common enough to make it a language requirement.


Of course you are right in saying that a runtime operator for
getting an object's type doesn't make much sense in C.

I am not sure, however, if I would characterize gcc's typeof()
as a "band-aid for macros". Calling it band-aid implies that
there may be a better solution in sight. Look at:

#define swap(a,b) do \
{ typeof(a) swap_tmp = a; a = b; b = swap_tmp; } while(0)

The alternative is to write a different swap macro/function for
each different type. Which is the cleaner? Which is the band-aid?

A more interesting and useful application of a typeof()
construct would be in the dynamic allocation of two-dimensional
arrays. Instead of having to supply multiple functions such as:

double **make_matrix_double (size_t m, size_t n);
int **make_matrix_int (size_t m, size_t n);
complex **make_matrix_complex (size_t m, size_t n);
mystruct **make_matrix_mystruct(size_t m, size_t n);

you may write a single make_matrix() macro for all types.
Very handy for numerical analysts working with matrix algebra.
I wouldn't dismiss this an "odd occasion where it is useful".

--
Rouben Rostamian

Nov 14 '05 #13
On Mon, 15 Nov 2004 15:36:05 +0000, Rouben Rostamian wrote:

....
I am not sure, however, if I would characterize gcc's typeof()
as a "band-aid for macros". Calling it band-aid implies that
there may be a better solution in sight. Look at:
It is something to squeeze a bit more functionality out of
macros. I don't see the implication of something better in sight.
#define swap(a,b) do \
{ typeof(a) swap_tmp = a; a = b; b = swap_tmp; } while(0)
I've never seen much use for swap macros myself. When application
code does enough swapping to warrant one it is usually over a
small number of types.
The alternative is to write a different swap macro/function for
each different type. Which is the cleaner? Which is the band-aid?
Maybe both are band-aids.
A more interesting and useful application of a typeof()
construct would be in the dynamic allocation of two-dimensional
arrays. Instead of having to supply multiple functions such as:

double **make_matrix_double (size_t m, size_t n);
int **make_matrix_int (size_t m, size_t n);
complex **make_matrix_complex (size_t m, size_t n);
mystruct **make_matrix_mystruct(size_t m, size_t n);

you may write a single make_matrix() macro for all types.
Very handy for numerical analysts working with matrix algebra.
I wouldn't dismiss this an "odd occasion where it is useful".


I'd say that's precisely what it is. :-)

I didn't claim that band-aids aren't useful. But are examples like this
enough to warrant an extra feature in the language?

IIRC typeof seemed like a good idea to me in 1999, but I don't
see its failure to get into C99 as a great problem.

Lawrence
Nov 14 '05 #14
ga*****@yin.interaccess.com (Kenny McCormack) writes:
[...]
Stick around for a bit, and you will come to understand more things about
this very religious newsgroup.

One of the fundamental tenents of this religion is that if it is not
standard, it is *not C* (1). That is, the claim is not that it is merely
"not standard C", but that it is not C at all. Bog knows what it is (for
all they care, it might be Fortran or Frobozz or your left ear), but
neither of the following are in any sense written or in any way related to
the C language:

socket(a,b,c);
or messagebox(lpWhatever,OtherWeirdWindowsParameterTy pe,etc,etc);


I don't think most of us would claim that non-standard C is "not C".
After all, the language itself allows for extensions, and library
functions like socket() and messagebox() could well be written in the
language itself.

But that's not the point. The point is that non-standard C is not
what we discuss in this newsgroup. There are newsgroups for Unix,
Windows, gcc, etc. where these things are topical, and you can find
people who actually know about them. There's nothing wrong with using
non-standard features when they're appropriate, we just can't help you
with them here. As you can see from the level of traffic, standard C
leaves us plenty to discuss.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #15
Robert Gamble <rg*******@gmail.com> wrote in message news:<pa****************************@gmail.com>...
On Sun, 14 Nov 2004 21:49:14 -0500, Joe Wright wrote:
Michael McGarry wrote:
Rouben Rostamian wrote:

In article <T9Bld.58779$EZ.51910@okepread07>,
Michael McGarry <mm******@nospam.org> wrote:

> Hi,
>
> I am pretty sure this is not possible, but maybe somehow it is.
>
> Given a variable, can I tell what type it is at runtime?

Some compilers, like gcc, provide a construct named
typeof() which produces the object type. Unfortunately,
(and inexplicably) typeof() has not found its way into
the C standard.

Is there really a typeof()?? Which standard library is it in?

Michael


No typeof in C.


I don't understand the confusion/sarcasm here. Rouben clearly stated it
was not standard. It is however a useful feature available to many
developers and there is no harm voicing the fact that it exists (along
with the fact that it is not standard as Rouben did) when there is no
standard solution and the information may help the OP.


What sarcasm? Rouben said it wasn't standard. Michael appeared to miss
that since he asked what standard header it aapeared in. Joe restated
that it's not part of C as such. What's your problem?
Nov 14 '05 #16
ga*****@yin.interaccess.com (Kenny McCormack) wrote in message news:<cn**********@yin.interaccess.com>...
In article <pa****************************@gmail.com>,
Robert Gamble <rg*******@gmail.com> wrote:
...
I don't understand the confusion/sarcasm here. Rouben clearly stated it
was not standard. It is however a useful feature available to many
developers and there is no harm voicing the fact that it exists (along
with the fact that it is not standard as Rouben did) when there is no
standard solution and the information may help the OP.
Stick around for a bit, and you will come to understand more things about
this very religious newsgroup.

One of the fundamental tenents of this religion is that if it is not
standard, it is *not C* (1). That is, the claim is not that it is merely
"not standard C", but that it is not C at all.


If you disagree with this, define C.
Bog knows what it is (for
all they care, it might be Fortran or Frobozz or your left ear),
It's an extended form of C, or a similar language based on C.
but neither of the following are in any sense written or in any way
related to the C language:

socket(a,b,c);
or messagebox(lpWhatever,OtherWeirdWindowsParameterTy pe,etc,etc);


Don't talk nonsense. If the poster provides declarations for the
functions and definitions of any non-standard types used, then
there is plenty to discuss about such things. For example,
discussion of whether or not the parameters being passed have
the appropriate types would be fine. What is obviously nothing
to do with C is discussion of why the call didn't make a message
box appear in the expected place, for example.
Nov 14 '05 #17
Rouben Rostamian <ro****@pc18.math.umbc.edu> wrote:
#define swap(a,b) do \
{ typeof(a) swap_tmp = a; a = b; b = swap_tmp; } while(0) The alternative is to write a different swap macro/function for
each different type.


For this and similar kind of macros I pass the type in argument:

#define SWAP(type, a, b) do \
{ type swap_tmp = a; a = b; b = swap_tmp; } while(0)

SWAP(struct mystruct, a, b);

(And I miss typeof() sometimes, too.)

--
Stan Tobias
sed 's/[A-Z]//g' to email
Nov 14 '05 #18
On Mon, 15 Nov 2004 15:02:56 -0800, J. J. Farrell wrote:
Robert Gamble <rg*******@gmail.com> wrote in message
news:<pa****************************@gmail.com>...
On Sun, 14 Nov 2004 21:49:14 -0500, Joe Wright wrote:
> Michael McGarry wrote:
>> Rouben Rostamian wrote:
>>
>>> In article <T9Bld.58779$EZ.51910@okepread07>, Michael McGarry
>>> <mm******@nospam.org> wrote:
>>>
>>>> Hi,
>>>>
>>>> I am pretty sure this is not possible, but maybe somehow it is.
>>>>
>>>> Given a variable, can I tell what type it is at runtime?
>>>
>>>
>>>
>>> Some compilers, like gcc, provide a construct named typeof() which
>>> produces the object type. Unfortunately, (and inexplicably)
>>> typeof() has not found its way into the C standard.
>>>
>> Is there really a typeof()?? Which standard library is it in?
>>
>> Michael
>
> No typeof in C.


I don't understand the confusion/sarcasm here. Rouben clearly stated it
was not standard. It is however a useful feature available to many
developers and there is no harm voicing the fact that it exists (along
with the fact that it is not standard as Rouben did) when there is no
standard solution and the information may help the OP.


What sarcasm? Rouben said it wasn't standard. Michael appeared to miss
that since he asked what standard header it aapeared in. Joe restated
that it's not part of C as such. What's your problem?


I said confusion/sarcasm, as in I didn't know whether he was confused or
being sarcastic. Given the reaction of most people here when someone
discusses something non-standard I don't think is unreasonable to assume
the latter (which I didn't).

Rob Gamble
Nov 14 '05 #19
ro****@pc18.math.umbc.edu (Rouben Rostamian) wrote in message news:<cn**********@pc18.math.umbc.edu>...
In article <pa***********************@netactive.co.uk>,
Lawrence Kirby <lk****@netactive.co.uk> wrote:
On Sun, 14 Nov 2004 13:32:08 +0000, Rouben Rostamian wrote:
In article <T9Bld.58779$EZ.51910@okepread07>,
Michael McGarry <mm******@nospam.org> wrote:
Hi,

I am pretty sure this is not possible, but maybe somehow it is.

Given a variable, can I tell what type it is at runtime?
How would you want this information to be represented? How would you use
it? Remember that C is statically typed. You know at the point you use
a variable what type it is, you just have to look at the definition for
the variable.
Some compilers, like gcc, provide a construct named
typeof() which produces the object type.


AFAIK this is just a band-aid for macros, replicating a type in
another declaration or a cast - strictly a compile time thing. It doesn't
address the OP's question of a runtime test. Since C doesn't support
Runtime Type Information, inheritance or any sort of dynamic typing a
runtime test doesn't make much sense - it just tells you what you already
know.
Unfortunately,
(and inexplicably) typeof() has not found its way into
the C standard.


There may be the odd occasion where it is useful but don't see that these
are common enough to make it a language requirement.


Of course you are right in saying that a runtime operator for
getting an object's type doesn't make much sense in C.

I am not sure, however, if I would characterize gcc's typeof()
as a "band-aid for macros". Calling it band-aid implies that
there may be a better solution in sight. Look at:

#define swap(a,b) do \
{ typeof(a) swap_tmp = a; a = b; b = swap_tmp; } while(0)

The alternative is to write a different swap macro/function for
each different type. Which is the cleaner? Which is the band-aid?


One more alternative:reading up on K&R, they did it years ago without
any typeof operator.Hint:Pass the type as a parameter.
A more interesting and useful application of a typeof()
construct would be in the dynamic allocation of two-dimensional
arrays. Instead of having to supply multiple functions such as:

double **make_matrix_double (size_t m, size_t n);
int **make_matrix_int (size_t m, size_t n);
complex **make_matrix_complex (size_t m, size_t n);
mystruct **make_matrix_mystruct(size_t m, size_t n);

you may write a single make_matrix() macro for all types.
Very handy for numerical analysts working with matrix algebra.
I wouldn't dismiss this an "odd occasion where it is useful".


HTH.
Suman.
Nov 14 '05 #20
In <2v*************@uni-berlin.de> "S.Tobias" <sN*******@amu.edu.pl> writes:
Rouben Rostamian <ro****@pc18.math.umbc.edu> wrote:
#define swap(a,b) do \
{ typeof(a) swap_tmp = a; a = b; b = swap_tmp; } while(0)

The alternative is to write a different swap macro/function for
each different type.


For this and similar kind of macros I pass the type in argument:

#define SWAP(type, a, b) do \
{ type swap_tmp = a; a = b; b = swap_tmp; } while(0)

SWAP(struct mystruct, a, b);


If you pass the temp object instead, the macro definition is much
simpler and invoking the thing really looks like a function call:

#define SWAP(a, b, t) (t = a, a = b, b = t)

SWAP(a, b, tmp);

Most of the time you already have an object of the right type available
to be used as the third argument.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #21
Dan Pop <Da*****@cern.ch> wrote:
In <2v*************@uni-berlin.de> "S.Tobias" <sN*******@amu.edu.pl> writes:
#define SWAP(type, a, b) do \
{ type swap_tmp = a; a = b; b = swap_tmp; } while(0)

SWAP(struct mystruct, a, b);

If you pass the temp object instead, the macro definition is much
simpler and invoking the thing really looks like a function call: #define SWAP(a, b, t) (t = a, a = b, b = t) SWAP(a, b, tmp);


Agreed, but still I prefer passing the type rather than an object.
Here's why:

Some algorithms might require more than one temp object. Having
to pass different number of temp arguments to different macros
would be clumsy. Passing the type I gain *uniform interface*.

When changing the algorithm, the new one could require more
temp objects; in this case I would have to change number of
parameters and subsequently change all macro calls. When
I pass the type, macro expansion can generate as many local
temp variables as it needs. This way I achieve *stable interface*.

In both methods you have to know the type - you have to write
it either at temp object declaration or as an argument to macro.
If typeof() was standard, we wouldn't have to think about the
type. Anyway, my method a little resembles C++ templates:
template<class type> void swap(type a, type b) { type t=a; a=b; b=t; }
swap<int>(x, y); <-> SWAP(int, x, y);

I think the difference can be perceived when constructing macros
with nested macro calls: it's extremely convenient to just pass the
type:
#define MACRO(type, x, y) do{ MAC_A(type, x); MAC_B(type, x, y); }while(0)
#define MAC_A(type, x) /*...*/
#define MAC_B(type, x, y) /*...*/

And, of course, inside a macro definition I wouldn't name a temp
object "swap_tmp", but rather "swap_tmp_200411172322" (my efficiency
at present is below one temporary variable per minute).

--
Stan Tobias
sed 's/[A-Z]//g' to email
Nov 14 '05 #22
In <30*************@uni-berlin.de> "S.Tobias" <sN*******@amu.edu.pl> writes:
Dan Pop <Da*****@cern.ch> wrote:
In <2v*************@uni-berlin.de> "S.Tobias" <sN*******@amu.edu.pl> writes:
>#define SWAP(type, a, b) do \
> { type swap_tmp = a; a = b; b = swap_tmp; } while(0)
>
>SWAP(struct mystruct, a, b);

If you pass the temp object instead, the macro definition is much
simpler and invoking the thing really looks like a function call:

#define SWAP(a, b, t) (t = a, a = b, b = t)

SWAP(a, b, tmp);


Agreed, but still I prefer passing the type rather than an object.
Here's why:

Some algorithms might require more than one temp object.


Such algorithms are highly unlikely to get implemented as macros.
When changing the algorithm, the new one could require more
temp objects;


When was the last time you've made such a change to a macro?

If it's not something very straightforward, you probably don't want to
implement it as a macro. Otherwise, debugging and maintenance become
a nightmare.

Multiple temps are nice when you want to write macros with true function
call semantics, to avoid multiple evaluation of the macro parameters.
However, I can happily live with multiple evaluations and never include
side effects in the expressions passed as arguments to macros. I usually
avoid such expressions even in genuine function calls.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #23
In article <Pi**********************************@unix43.andre w.cmu.edu>,
Arthur J. O'Dwyer <aj*@nospam.andrew.cmu.edu> wrote:

On Sat, 13 Nov 2004, Michael McGarry wrote:

Given a variable, can I tell what type it is at runtime?


Sure. Just take the type it was at compile time, and remember it.
For example,

int main(void)
{
int i;
printf("i is of type %s.\n", "int");
return 0;
}

If you have some other idea in mind, you're going to have to be more
explicit.


It would be interesting to know what the OP really had in mind.
Unfortunately, seems to be a case of "post-and-run".

Maybe he is thinking of textual analysis on the source code. Like, here's
a bunch of *.c files - what type is variable "foo".

Nov 14 '05 #24
In <pa***********************@netactive.co.uk> Lawrence Kirby <lk****@netactive.co.uk> writes:
On Sun, 14 Nov 2004 13:32:08 +0000, Rouben Rostamian wrote:
In article <T9Bld.58779$EZ.51910@okepread07>,
Michael McGarry <mm******@nospam.org> wrote:

Given a variable, can I tell what type it is at runtime?
How would you want this information to be represented? How would you use
it? Remember that C is statically typed. You know at the point you use
a variable what type it is, you just have to look at the definition for
the variable.
Some compilers, like gcc, provide a construct named
typeof() which produces the object type.
AFAIK this is just a band-aid for macros, replicating a type in
another declaration or a cast - strictly a compile time thing. It doesn't
address the OP's question of a runtime test.


True. The GNU C feature for runtime type testing is the function
__builtin_types_compatible_p(type1, type2) and not the typeof operator.

It is impossible to implement things like <tgmath.h> without this kind of
tool. And it's quite handy for writing your own type generic macros, but,
unfortunately, the C standard doesn't also provide the tools required for
implementing its own headers.
Since C doesn't support
Runtime Type Information, inheritance or any sort of dynamic typing a
runtime test doesn't make much sense - it just tells you what you already
know.


Not true when implementing macros with parameters.
Unfortunately,
(and inexplicably) typeof() has not found its way into
the C standard.


There may be the odd occasion where it is useful but don't see that these
are common enough to make it a language requirement.


typeof is a must for any *decent* type generic macro needing temporary
variables. Without it, the macro needs additional arguments specifying
either the types of the temps or the names of the temps. Trivial
example: a type generic SWAP(x, y).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #25
In <pa****************************@netactive.co.uk> Lawrence Kirby <lk****@netactive.co.uk> writes:
I didn't claim that band-aids aren't useful.
You can consider the C language itself as a band-aid for people who cut
themselves with assembly.
But are examples like this
enough to warrant an extra feature in the language?
Well, they're far more useful to me than most of the new features of C99.
IIRC typeof seemed like a good idea to me in 1999, but I don't
see its failure to get into C99 as a great problem.


It is such failures that explain the failure of C99 as a whole. If C99
actually provided most extensions *C* programmers keep asking for, the
demand for C99 implementations would have been high enough to have C89
actually replaced by C99 in the real world.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #26
Kenny McCormack wrote:
<snip>

Stick around for a bit, and you will come to understand more things about
this very religious newsgroup.

One of the fundamental tenents of this religion is that if it is not
standard, it is *not C* (1). That is, the claim is not that it is merely
"not standard C", but that it is not C at all. Bog knows what it is (for
all they care, it might be Fortran or Frobozz or your left ear), but
neither of the following are in any sense written or in any way related to
the C language: <snip>

Here, "C" is commonly used as shorthand for "standard C". Are you still
struggling with this concept?
(1) And that fact is enforced by a bunch of religious Nazis (get it, not C,
nazi, heh heh)


How horribly unbearable! I'll have mama get us some ginger snaps and
warm milk before bedtime!

Mark F. Haigh
mf*****@sbcglobal.net
Nov 14 '05 #27

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: fotzor | last post by:
Hi, is it somehow possible to get the type of a variable in a macro? example: #define SOME_MACRO(x) // what's the type of x??? i guess the type-information is lost but i can't use a template...
11
by: Ray Muforosky | last post by:
I have this: ------------ print "<FORM name=\"form3\" ACTION=\"cmdlog_rep.php\">\n"; print "<TD><INPUT TYPE=\"submit\" VALUE=\"Submit\"></TD>\n"; .. print "<INPUT type=\"HIDDEN\"...
4
by: gimme_this_gimme_that | last post by:
Hi, This is sort of a : How to build a Yes/No dialog box qquestion. Or perhaps a question about getting javascript variables from a pop-up window and processing them on a submit. This is...
3
by: Mike Spertus | last post by:
Is there a way to get an approximate stackpointer into an int in C#? The following works, but is unsafe and adds a new variable to the stackframe: int i = (int)(&i); It seems like it should...
20
by: Shawnk | last post by:
I would like to get the class INSTANCE name (not type name) of an 'object'. I can get the object (l_obj_ref.GetType()) and then get the (l_obj_typ.Name) for the class name. I there any way of...
4
by: R.Manikandan | last post by:
Hi In my code, one string variable is subjected to contain more amount of characters. If it cross certain limit, the string content in the varabile is automatically getting truncated and i am...
41
by: Jim | last post by:
Hi guys, I have an object which represents an "item" in a CMS "component" where an "item" in the most basic form just a field, and a "component" is effectively a table. "item" objects can be...
1
by: simbarashe | last post by:
Hie could someone please help me with getting and using the current page url. I have a function that gets the url, I want to use it with header(location : XXX) but it wont work. The code is as...
15
by: ssylee | last post by:
I am using mikroC to program some microcontroller code in C. However I have come across of potential problem of bool type not being supported from the compiler complaint on how I declared type bool...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.