473,765 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
26 2438
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@okepread 07>,
Michael McGarry <mm******@nospa m.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************ ***********@net active.co.uk>,
Lawrence Kirby <lk****@netacti ve.co.uk> wrote:
On Sun, 14 Nov 2004 13:32:08 +0000, Rouben Rostamian wrote:
In article <T9Bld.58779$EZ .51910@okepread 07>,
Michael McGarry <mm******@nospa m.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_d ouble (size_t m, size_t n);
int **make_matrix_i nt (size_t m, size_t n);
complex **make_matrix_c omplex (size_t m, size_t n);
mystruct **make_matrix_m ystruct(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_d ouble (size_t m, size_t n);
int **make_matrix_i nt (size_t m, size_t n);
complex **make_matrix_c omplex (size_t m, size_t n);
mystruct **make_matrix_m ystruct(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.int eraccess.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(lpWh atever,OtherWei rdWindowsParame terType,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_Keit h) 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*******@gmai l.com> wrote in message news:<pa******* *************** ******@gmail.co m>...
On Sun, 14 Nov 2004 21:49:14 -0500, Joe Wright wrote:
Michael McGarry wrote:
Rouben Rostamian wrote:

In article <T9Bld.58779$EZ .51910@okepread 07>,
Michael McGarry <mm******@nospa m.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.int eraccess.com (Kenny McCormack) wrote in message news:<cn******* ***@yin.interac cess.com>...
In article <pa************ *************** *@gmail.com>,
Robert Gamble <rg*******@gmai l.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(lpWh atever,OtherWei rdWindowsParame terType,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.ma th.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*******@gmai l.com> wrote in message
news:<pa******* *************** ******@gmail.co m>...
On Sun, 14 Nov 2004 21:49:14 -0500, Joe Wright wrote:
> Michael McGarry wrote:
>> Rouben Rostamian wrote:
>>
>>> In article <T9Bld.58779$EZ .51910@okepread 07>, Michael McGarry
>>> <mm******@nospa m.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.mat h.umbc.edu (Rouben Rostamian) wrote in message news:<cn******* ***@pc18.math.u mbc.edu>...
In article <pa************ ***********@net active.co.uk>,
Lawrence Kirby <lk****@netacti ve.co.uk> wrote:
On Sun, 14 Nov 2004 13:32:08 +0000, Rouben Rostamian wrote:
In article <T9Bld.58779$EZ .51910@okepread 07>,
Michael McGarry <mm******@nospa m.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:rea ding up on K&R, they did it years ago without
any typeof operator.Hint:P ass 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_d ouble (size_t m, size_t n);
int **make_matrix_i nt (size_t m, size_t n);
complex **make_matrix_c omplex (size_t m, size_t n);
mystruct **make_matrix_m ystruct(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

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

Similar topics

5
1896
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 here
11
1927
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\" name=\"year1\" >\n"; print "<INPUT type=\"TEXT\" name=\"when\" >\n"; print "</FORM>\n";
4
4938
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 what I'd like to have happen :
3
1918
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 be possible to do this safely because I never dereference a pointer. Perhaps there is a safe way to load a register value into an int?
20
27795
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 getting 'l_Fred_my_ins' out of the following. .... My_cls l_Fred_my_ins = new My_cls();
4
4508
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 not getting the full data. Is there any max limit for an variable to store in javascript? Can anyone please help me to solve this prob?
41
2885
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 created and then added to "component" objects to build up the component definition. My dilemma comes in deciding how to read/write data to the "item"
1
3199
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 follows: The code below is for the first page:session_start is in line 3 <link href="css/jobSheet.css" rel="stylesheet" type="text/css" /> session_start();
15
4352
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 variable. From a forum thread that I've pulled out from mikroBasic, I realized that bool type isn't fully supported. When programming in mikroC, I have been getting an error message of "invalid expression" when I declared a bool type variable,...
0
10168
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10008
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9837
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8833
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.