Has a way to get the datatype in C?
For example:
int foo; if (foo is int) { // do something }
or something like:
if (typeof(foo) == typeof(int)) { // do something }
Thanks in advance.
The typeof() method It returns the data type of that variable.
You can print all of the normal C types with printf by using different placeholders: int (integer values) uses %d. float (floating point values) uses %f. char (single character values) uses %c.
typeof is a JavaScript keyword that will return the type of a variable when you call it. You can use this to validate function parameters or check if variables are defined. There are other uses as well. The typeof operator is useful because it is an easy way to check the type of a variable in your code.
To get the datatype of variable, use typeid(x). name() of typeinfo library. It returns the type name of the variable as a string.
This is called type introspection or reflection and is not supported by the C language. You would probably have to write your own reflection library, and it would be a significant effort.
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