tags:

views:

93

answers:

3

I want to print out (or otherwise ascertain) the type of some variable in my program. Is there a good way to do it? By good, I mean a way that works, even if it means intentionally throwing compiler errors.

For example:

client.c:55: error: incompatible types in assignment

is the error I'm getting right now. What I WANT is it to tell me something like:

client.c:55: error: attempting to assign type struct a to type struct b

or a function that I can use like so:

printf(gettype(x));

which would output:

struct b
+1  A: 

try debugging using GDB, it will print all properties associated with the variable including it's type. But, your program should compile before using GDB.

Manav MN
A: 

If you're using gcc or a gcc-compatible compiler then you can use the (obviously non-standard and non-portable) typeof keyword, which works much like sizeof.

Paul R
Unfortunately you can't get a string out of `typeof`. (Though on g++ you can use `typeid(typeof(x)).name()`.)
KennyTM
+1  A: 

In C you provide a type when you declare a variable. That is the only information that the compiler has when it is complaining about the assignment (that is, it will not use the runtime type of the object, but the static type you have).

Go to the code, locate line 55, check what variables are there and find the types in the code. In C there are not even overloads, types are as static and simple as it gets in any language.

David Rodríguez - dribeas