cpp get data type

#include <iostream>
using namespace std;

template <typename T>
void getDataType(T var) {
    cout << "The data type is: " << typeid(var).name() << endl;
}

int main() {
    int integerVar = 10;
    double doubleVar = 3.14;
    char charVar = 'A';

    getDataType(integerVar);
    getDataType(doubleVar);
    getDataType(charVar);

    return 0;
}

Explanation:

  1. #include <iostream>: Includes the input/output stream library that allows input and output operations.
  2. using namespace std;: Specifies that the program will use the standard namespace, allowing access to functionality like cout.
  3. template <typename T> void getDataType(T var) {...}: Defines a template function getDataType that takes a parameter var of a generic type T.
  4. cout << "The data type is: " << typeid(var).name() << endl;: Outputs the message "The data type is: " along with the name of the type of the variable var.
  5. int main() {...}: The main function where the program execution begins.
  6. int integerVar = 10; double doubleVar = 3.14; char charVar = 'A';: Declares variables integerVar, doubleVar, and charVar of types int, double, and char respectively, and initializes them with values.
  7. getDataType(integerVar); getDataType(doubleVar); getDataType(charVar);: Calls the getDataType function with the variables integerVar, doubleVar, and charVar as arguments to determine their respective data types using typeid.

  8. return 0;: Indicates successful program execution by returning 0 to the operating system.