getting variable type in C

M

Michael McGarry

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
 
A

Arthur J. O'Dwyer

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
 
H

Herbert Rosenau

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.
 
M

Mark McIntyre

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.
 
Z

zyBitX

Michael McGarry said:
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 ?...
 
R

Rouben Rostamian

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.
 
M

Michael McGarry

Rouben said:
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
 
R

Robert Gamble

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
 
T

Trent Buck

Quoth Michael McGarry on or about 2004-11-14:
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
 
K

Kenny McCormack

Robert Gamble said:
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. 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,OtherWeirdWindowsParameterType,etc,etc);

HTH.

(1) And that fact is enforced by a bunch of religious Nazis (get it, not C,
nazi, heh heh)
 
L

Lawrence Kirby

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
 
R

Rouben Rostamian

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.


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.


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".
 
L

Lawrence Kirby

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
 
K

Keith Thompson

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,OtherWeirdWindowsParameterType,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.
 
J

J. J. Farrell

Robert Gamble said:
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?
 
J

J. J. Farrell

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,OtherWeirdWindowsParameterType,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.
 
S

S.Tobias

Rouben Rostamian said:
#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.)
 
R

Robert Gamble

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,154
Messages
2,570,870
Members
47,400
Latest member
FloridaFvt

Latest Threads

Top