views:

124

answers:

6

I have several variables of type char (array), int, and double. Is there a way to identify what type they are during run-time?

For example, I'm looking for something like:

int dummyInt = 5;    
double dummyDouble = 5.0;    
dummyInt == int ?
        printf("yes, it's of int type\n") : printf("no, it's not of int type\n");
dummyDouble == int ?
        printf("yes, it's of int type\n") : printf("no, it's not of int type\n");

Where the obvious results will be:

yes, it's of int type
no, it's not of int type

Well, the reason why I need it is because I'm transferring data from variables into an SQL database (using SQLite). Now, the headers can change each time I run the program depending on which variables are being used. So when I'm creating the table, I need to tell it if it's VARCHAR, INTEGER, DOUBLE etc.

A: 

No, there is no way to do it in C.

Donotalo
+5  A: 

No, you can't, but why do you even want to do this? C is statically typed, so when you declare int dummyInt = 5, you already know that it's an int.

Edit:

Now, the headers can change each time I run the program depending on which variables are being used. So when I'm creating the table, I need to tell it if it's VARCHAR, INTEGER, DOUBLE etc.

That still doesn't change much. The types are still known at compile-time, so maybe all you need is a #define alongside the actual variable that defines the SQL type, say:

int someVar;
#define SOMEVAR_SQL_TYPE "INTEGER"
casablanca
A: 

completely out of spec but try here for one example

KevinDTimm
A: 

I had the same problem but using C++ This is not C but perhaps an equivalent exists. I had to use the header then call typeid();

My code looked like:

#include typeinfo

int main()
{ 
    int a = 10;
    cout << typeid(int).name() << endl;
}

I know you can share header files so perhaps it can be made to work?

The output of typeid used a letter for each type (implementation specific). i.e. an unsigned int was output as a 'j'.

Here is my orignal question, similar to your problem.

aLostMonkey
purely c++ (which also includes a variety of typeof)
KevinDTimm
+1  A: 

I assume you want to have a program where you don't know the type of an object until runtime. For example, you want to implement a polymorphic sorting algorithm which doesn't know the type of the object until runtime (at which point, it knows only the size of each element because C is statically typed, but even this may break down unless you implement your own functions which are independent of all types and does all of the bit arithmetic for the objects).

If you do want to do some sort of polymorphic approach for something described above, I recommend you use char pointers and allocate enough space on the heap for whatever size is needed at runtime. Then implement functions which receive a pointer to the space, the element size, and number of elements, and performs the appropriate comparison/arithmetic operations. This implementation can get very complicated. It may not be complicated if you supply a module function which does know the type, but then you are back to knowing the type at compile time.

Kizaru
+1  A: 

There is no facility in standard C to obtain the type of a variable at runtime; you are expected to do all your own bookkeeping in that regard.

If you need to create a generic container in C, you will have to figure out some way of tagging each element in the container with some sort of type information. Depending on the approach, it can be a lot of work.

John Bode