get type of variable in c++
To get the type of a variable in C++, you can use the typeid
operator along with the name()
function. Here are the steps to do it:
- Include the
<typeinfo>
header file in your program to use thetypeid
operator and thename()
function.
#include <typeinfo>
- Declare a variable whose type you want to determine.
int num = 10;
- Use the
typeid
operator followed by thename()
function to get the type of the variable. Assign the result to aconst char*
variable.
const char* typeName = typeid(num).name();
- Print the type name using the
typeName
variable.
std::cout << "Type of variable: " << typeName << std::endl;
By executing these steps, you will be able to obtain the type of a variable in C++.