+ 6
How to check the type of a variable in C
Hello, I want to check the type of a variable in C. I searched through the internet but it seems to be no solution. There's no function like 'typeof' in C as far as I now. Please let me know if there's any answer. Thank you.
3 odpowiedzi
+ 3
Thanks a lot @ace and @vlad. I really appreciate your answers.
+ 1
Simple solution:
Go to the beginning of the function, look to see how the variable is defined and name variables better in the future.
Complex solution:
enum t_typename {
TYPENAME_BOOL
TYPENAME_INT
...
} //You must define every single type. Unsigned int is different from singed int which is different from int.
#define typename(x) _Generic((x), \
\
_Bool: TYPENAME_BOOL, \
int: TYPENAME_INT \
...
)
+ 1
@Ace Using sizeof is a bad solution as it could easily produce false positives (especially when checking to see if floating-point numbers are integers or vice-versa).