Tuesday, March 17, 2015

How to Get type name of a variable in C++

#include<typeinfo>
#include<iostream>
#include<conio.h>
#include<string>

using namespace std;

void main()
{
int nIntNum = 10;
float fFloatNum = 20.5;

std::string strStringVar = "Iam String";

double dDouble = 2.001;

cout <<"Iam :"<< typeid(nIntNum).name()<<endl;
cout <<"Iam :"<< typeid(fFloatNum).name() << endl;
cout <<"Iam :"<< typeid(strStringVar).name() << endl;
cout << "Iam :" << typeid(dDouble).name() << endl;

_getch();

}

Output :

Iam : int
Iam : float
Iam : class std::basic_string
Iam : double

No comments:

Post a Comment