infyash--
infyash--

Reputation: 23

Is it possible to find the datatype of a variable in C?

I know it is very clearly mentioned during input operation but want to know . Like in Python3.6 we have "type()" function. Just curious about it.

Upvotes: 2

Views: 1445

Answers (4)

n. m. could be an AI
n. m. could be an AI

Reputation: 120229

Python variables don't have types. Python values have types. Therefore it is sometines necessary to use type(): the same variable can hold values of different types, and the programmer doesn't necessarily know which type is currently being held.

p = foo() 
print (type(p)) # could be any type
p = bar() # the type can change
p = baz() # and again

C variables have types. They are statically known and explicitly declared by the programmer. The type of the value is the type of the variable. Therefore something like type() would be redundant. For a given variable, the tyoe is always the same. It is always possible to look at the declaration. With an IDE, this can be done in one click, no matter where the declaration is.

int p = foo(); // the type of p is int
p = bar(); // the type of p is int
p = baz(); // guess what, still int

There is more to this subject, e.g. macros, generics, void pointers, casts, dynamically allocated objects and more. These are all somewhat advanced topics though. One should unserstand the basics first.

Upvotes: 0

No, nothing of the sorts of type() is available in C. In Python the variables do not have types. They all are references - or pointers if you will - to the actual objects. This is unlike in C where variables too are objects themselves

The type of the objects is often the type of the lvalue used to access the memory. All tricks given in the answers here only find the type compile-time type i.e. the type of the lvalue or value of expression - all that is compile-type information - the compiler, while processing the file, can find out what those types are since they were declared there, i.e. they can find out the type of the variable.

But that is not what the Python type function does: type can find out the type identity of the pointed-to object:

def foo(i):
    if type(i) is int:
        ...

In here it is not the type of i that is being compared against int but the type of that object that is currently bound to that name i.

In C if you cast a pointer to object to void *, there's nothing to recover that information from. For example:

void do_something(void *p) {
    // ???
}

...

    int *a = malloc(sizeof int);
    *a = 42;
    void *p = a;
    do_something(p);

the p points to an object of (effective) type int with current value 42, but it is of type void *. There is absolutely no generic mechanism in C for do_something to find out, given p, that it points to an object of type int. It just needs to be known.

Upvotes: 1

Jose
Jose

Reputation: 3460

Another possibility is to create your own variables manager through the precompiler:

#include <stdio.h>

#define STR1(x)  #x
#define STR(x)  STR1(x)
#define variable(x,name) x var_##name; char* type_##name = STR(x)

int main() { 
    variable(float, testF);
    variable(int, testI);
}

See here

Upvotes: 0

Shawn
Shawn

Reputation: 52644

You can do something with _Generic() from C11 as long as you know the types you care about ahead of time:

#include <stdio.h>

#define Type(x) _Generic((x),                   \
                         int: "int",            \
                         short: "short",        \
                         long: "long",          \
                         char: "char",          \
                         float: "float",        \
                         double: "double",      \
                         default: "unknown"     \
                         )

int main(void) {
  int i;
  short s;
  long lng;
  long long llng;
  char c;
  float f;
  double d;
  printf("%s\n", Type(i));
  printf("%s\n", Type(s));
  printf("%s\n", Type(lng));
  printf("%s\n", Type(llng));
  printf("%s\n", Type(c));
  printf("%s\n", Type(f));
  printf("%s\n", Type(d));
}

when compiled and run produces:

int
short
long
unknown
char
float
double

Upvotes: 6

Related Questions