473,749 Members | 2,665 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 #1
26 2430

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******@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?


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******@nospa m.org> wrote in message news:T9Bld.5877 9$EZ.51910@okep read07...
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******@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?


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******@nospa m.org> wrote in message news:<T9Bld.587 79$EZ.51910@oke pread07>...
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@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.

--
Rouben Rostamian
Nov 14 '05 #7
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
Nov 14 '05 #8
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.
--
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@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.

Rob Gamble
Nov 14 '05 #10

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

Similar topics

5
1893
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
1926
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
4937
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
1917
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
27792
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
4507
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
2883
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
4349
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
8997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8833
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9568
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...
1
9335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
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...
1
6801
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
6079
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
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2794
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.