How to check the type of a variable in C | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

25th Jan 2018, 4:27 AM
Chamin Jayasooriya
Chamin Jayasooriya - avatar
3 Answers
+ 3
Thanks a lot @ace and @vlad. I really appreciate your answers.
25th Jan 2018, 5:34 AM
Chamin Jayasooriya
Chamin Jayasooriya - avatar
+ 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 \ ... )
25th Jan 2018, 4:49 AM
Vlad Serbu
Vlad Serbu - avatar
+ 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).
25th Jan 2018, 5:12 AM
Vlad Serbu
Vlad Serbu - avatar