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
To get the type of a variable in Python, you can use the built-in type() function. In Python, everything is an object. So, when you use the type() function to print the type of the value stored in a variable to the console, it returns the class type of the object.
C is statically typed language. You can't declare a function which operate on type A or type B, and you can't declare variable which hold type A or type B. Every variable has an explicitly declared and unchangeable type, and you supposed to use this knowledge.
To print the address of a variable, we use "%p" specifier in C programming language. There are two ways to get the address of the variable: By using "address of" (&) operator. By using pointer variable.
Using printf function, we can print the value of a variable.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With