Find the Data Type of Variable

C

chellappa

Hi All,
How to find the data type of the variable ? is there any libaray
function avaiable? Because i want create generic data type of some
operation .
Thanks All
By
Chellappa
 
E

Eric Sosman

chellappa wrote On 01/31/06 10:42,:
Hi All,
How to find the data type of the variable ?

Remember it.
is there any libaray
function avaiable?

No.
Because i want create generic data type of some
operation .

If it's truly "generic" you don't need to know
the type. To the extent that type knowledge is needed,
you're not writing generic code.
 
M

Mike Wahler

chellappa said:
Hi All,
How to find the data type of the variable ?

The C language does not have a facility for doing this.
is there any libaray
function avaiable? Because i want create generic data type of some
operation .

When you create an object, you know its type. E.g.:

int i;
double d;

If you keep track of this information yourself (e.g. with
a 'type flag' or such), you can implement 'generic' types
using a pointer to void (type 'void *'). Use a 'type
flag' to convey type information to a function. This is
how 'printf()' does it (e.g. "%d" in the first argument
tells it that the associated argument is type' int). Of
course things won't work if you don't actually pass the
proper type. IOW, 'void*' is quite useful, but needs
to be used with great care. A sharp, two-edged knife.


-Mike
 
M

Michael Rasmussen

Hi All,
How to find the data type of the variable ? is there any libaray function
avaiable? Because i want create generic data type of some operation .
Inclose your data in a struct and parse the struct. The struct then needs
a variable holdind the type:

enum mytype {TYPE1, TYPE2, ...}

struct generic_type {
enum mytype thetype;
void* data;
}
 
S

Shastri

Hi,

I think it is compiler dependent but if you can find the size of the
variable, you can find the data type. But its not precise. Because
sometimes you may be dealing with data types which have the same size.
Not an exact procedure.

There is no library function in C.


Cheers
Shastri
 
K

Keith Thompson

Shastri said:
I think it is compiler dependent but if you can find the size of the
variable, you can find the data type. But its not precise. Because
sometimes you may be dealing with data types which have the same size.
Not an exact procedure.

There is no library function in C.

Please provide some context. Read <http://cfaj.freeshell.org/google/>.

Knowing the size of a variable isn't useful, except in *very* narrow
circumstances, if you need to know its type. For example, something
that's 4 bytes on one system I use might be any of:

int
unsigned int
long
unsigned long
float
short[2]
unsigned short[2]
char[4]
unsigned char[4]
signed char[4]
struct { short x; char y; unsigned char z; }

among almost infinitely many other possibilities.

There is no way in C to represent a type, unless you construct your
own type descriptor type. Unlike size, a variable's type is not a
run-time attribute that you can do anything with.

If you need to know the type of a variable, you need to remember how
you declared it.
 

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

Forum statistics

Threads
474,174
Messages
2,570,940
Members
47,485
Latest member
Andrewayne909

Latest Threads

Top