print data type of a variable in c++

To print the data type of a variable in C++, you can use the typeid operator and the name() function. Here are the steps to do it:

  1. Include the <typeinfo> header file at the beginning of your C++ program. This header file provides the necessary functions and operators for type identification.

  2. Declare the variable for which you want to print the data type.

  3. Use the typeid operator followed by the name() function to get the name of the data type. Pass the variable as an argument to the typeid operator.

  4. Print the name of the data type using the cout statement or any other method you prefer.

Here is an example code snippet that demonstrates these steps:

#include <iostream>
#include <typeinfo>

int main() {
    int num = 10;

    const std::type_info& type = typeid(num);
    std::cout << "Data type: " << type.name() << std::endl;

    return 0;
}

In this example, we have declared an int variable named num. We then use typeid(num) to obtain a reference to the type_info object that represents the data type of num. Finally, we print the name of the data type using type.name().

When you run this code, it will output the data type of the variable num, which in this case is int.