I need to find the specific data type for cases when I'm debugging C code, in particular on Arduino Studio and related platforms form MCU programming. How to retrieve the data type of a variable in c language?
Asked
Active
Viewed 311 times
1 Answers
0
There is a piece of code using compiler predefined macro that is able to print any type
template <class T>
String type_name(const T&)
{
String s = __PRETTY_FUNCTION__;
int start = s.indexOf("[with T = ") + 10;
int stop = s.lastIndexOf(']');
return s.substring(start, stop);
}
to use it one simply needs to call it like this:
double someFloat = 2.4535232;
const char* someString = "some string";
Serial.println(type_name(someFloat));
Serial.println(type_name(someString));
More detailed info can be found on Arduino Stack Exchange here: https://arduino.stackexchange.com/questions/3079/how-to-retrieve-the-data-type-of-a-variable

Miguel Tomás
- 1,714
- 1
- 13
- 23