How to identify the type of the variable in C?

3

In languages like Nodejs , Ruby , Moon , etc. When I want to know what the type of my variable is, just use the typeof function (not necessarily in the languages mentioned) that returns a String with the type name of my variable.

But how to do this in C?

    
asked by anonymous 23.02.2017 / 06:26

1 answer

4

The C language does not have a macro capable of doing this, but from gcc5 has the implementation of _Generic , which is able to issue a return from a type of variable.

#define typeof(var) _Generic( (var),\
char: "Char",\
int: "Integer",\
float: "Float",\
char *: "String",\
void *: "Pointer",\
default: "Undefined")

int main(){
    int x = 6;
    void *p = NULL;
    char *s = "text";
    printf("%s\n%s\n%s\n", typeof(x), typeof(p), typeof(s));
}

Output:

>Integer
>Pointer
>String

Reference: cpp-reference generic

    
23.02.2017 / 06:26