Detect datatype of variables in C

Insane LogsInsane Logs
2 min read

A simple Marco which uses generics in C to help you detect data type of any variable like you do with type() function in python πŸ™‚.

The code snippet contains examples on how to use it.
Extend it as much as you like πŸ™‚.

#include <stdio.h>
#include <stdlib.h>

#define TYPE_CHECK(var) _Generic((var),     \
    int             : "int",                \
    float           : "float",              \
    double          : "double",             \
    char            : "char",               \
    char*           : "string",             \
    int*            : "int pointer",        \
    float*          : "float pointer",      \
    double*         : "double pointer",     \
    char**          : "string pointer",     \
    void*           : "void pointer",       \
    short           : "short",              \
    long            : "long",               \
    unsigned int    : "unsigned int",       \
    unsigned long   : "unsigned long",      \
    unsigned short  : "unsigned short",     \
    default         : "unknown type"        \
)

int main(void) {
    // Basic types
    int a = 10;
    float b = 5.5f;
    double c = 3.14159;
    char d = 'A';
    char* str = "Hello, World!";

    // Pointer types
    int* p_a = &a;
    float* p_b = &b;
    double* p_c = &c;
    char** p_str = &str;
    void* p_void;

    // Print types using TYPE_CHECK
    printf("Type of a: %s\n",       TYPE_CHECK(a));         // Outputs "int"
    printf("Type of b: %s\n",       TYPE_CHECK(b));         // Outputs "float"
    printf("Type of c: %s\n",       TYPE_CHECK(c));         // Outputs "double"
    printf("Type of d: %s\n",       TYPE_CHECK(d));         // Outputs "char"
    printf("Type of str: %s\n",     TYPE_CHECK(str));       // Outputs "string"
    printf("Type of p_a: %s\n",     TYPE_CHECK(p_a));       // Outputs "int pointer"
    printf("Type of p_b: %s\n",     TYPE_CHECK(p_b));       // Outputs "float pointer"
    printf("Type of p_c: %s\n",     TYPE_CHECK(p_c));       // Outputs "double pointer"
    printf("Type of p_str: %s\n",   TYPE_CHECK(p_str));     // Outputs "string pointer"
    printf("Type of p_void: %s\n",  TYPE_CHECK(p_void));    // Outputs "void pointer"

    // Example of using malloc
    void* ptr = malloc(sizeof(int));
    printf("Type of ptr: %s\n",     TYPE_CHECK(ptr));       // Outputs "void pointer"

    // Good practice :)
    free(ptr);

    return 0;
}

Source (my blog website) : https://blog.insanelogs.xyz/posts/detect-datatype-of-variables-using-generics/

0
Subscribe to my newsletter

Read articles from Insane Logs directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Insane Logs
Insane Logs

Hi, I am Insane. Insanelogs - Log of insane Main website: https://www.insanelogs.xyz Blog website: https://blog.insanelogs.xyz My objective with InsaneLogs is to share knowledge and quick tutorials across different domains of computer science that I learn every day β€” in a simple and easy-to-understand manner. The goal is to help you get started with a topic quickly, and then dive deeper on your own. I believe it’s always better to have something working or functional first, before reading a ton of theory about it!