37

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
Ritwik Bose
  • 5,889
  • 8
  • 32
  • 43
  • how can it "work" if it doesnt compile ? – KeatsPeeks Jan 29 '10 at 08:08
  • If it "throws compiler errors", how can it work? – Alok Singhal Jan 29 '10 at 08:08
  • What I mean by "work" is it gives me the type so I can fix the error in my program. See the edit. – Ritwik Bose Jan 29 '10 at 08:09
  • Some compilers allow a verbosity setting - try that. Otherwise just look at the code. A decent IDE will usually automatically look up the types of variables so you can see the type by hovering or right clicking on the variable name. – Max Shawabkeh Jan 29 '10 at 08:12
  • 1
    To increase verbosity level, give -Wall -pedantic options to your gcc compiler. This should give you some additional info about your errors. – manav m-n Jan 29 '10 at 08:47

5 Answers5

81

I have just discovered how to do this.

printf("%d", variable);

If variable is not an int then gcc -Wall will complain that the types don't match - and will print out the type of the variable, which is exactly what you are looking for.

Ariel
  • 25,995
  • 5
  • 59
  • 69
  • Useful when you're in an environment where you don't know where to look stuff up (for example, in my situation, programming with yacc). – Casey Kuball Oct 03 '12 at 19:07
  • 12
    This is misleading when using array types that decay into pointers while being passed to `printf`. – Zulan Nov 18 '17 at 09:00
7

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 m-n
  • 11,136
  • 23
  • 74
  • 97
4

If you debug with gdb. Then you can set a breakpoint with: break line_number to break execution where you want to fetch that variable type. And from then you can print the type of a variable in gdb with: ptype your_variable_name or whatis your_variable_name The last one will print the type of the variable as well as the definition you gave it.

Kornee
  • 96
  • 5
2

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
  • 204,818
  • 23
  • 294
  • 489
1

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
  • 208,748
  • 37
  • 389
  • 560
  • 13
    Unfortunately you can't get a string out of `typeof`. (Though on g++ you can use `typeid(typeof(x)).name()`.) – kennytm Jan 29 '10 at 08:51
  • 1
    Since C11, you can also use `_Generic` to [to get the variable's type](https://stackoverflow.com/a/17290414/975097). – Anderson Green Jul 13 '21 at 03:40