Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could I retrieve the datatype from a variable in C?

Tags:

c

types

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.

like image 999
user464230 Avatar asked Feb 26 '11 15:02

user464230


People also ask

How do you find the variable data type?

The typeof() method It returns the data type of that variable.

How do you print the datatype of a variable in C?

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.

How will you check the data type of the value stored in variable?

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.

How do you determine the datatype of a variable in C++?

To get the datatype of variable, use typeid(x). name() of typeinfo library. It returns the type name of the variable as a string.


1 Answers

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.

like image 158
sourcenouveau Avatar answered Sep 28 '22 22:09

sourcenouveau