find data type in c++ code example

Example 1: how to check datatype of a variable in c++

#include <typeinfo>
...
cout << typeid(variable).name() << endl;

Example 2: cpp get data type

#include <typeinfo>
...
cout << typeid(variable).name() << endl;

Example 3: how to check type in c++

#include <typeinfo>
#include <iostream>

class someClass { };

int main(int argc, char* argv[]) {
    int a;
    someClass b;
    std::cout<<"a is of type: "<<typeid(a).name()<<std::endl; 
  	// Output 'a is of type int'
    std::cout<<"b is of type: "<<typeid(b).name()<<std::endl; 
  	// Output 'b is of type someClass'
    return 0;
  	// on the online compiler it comes as 'i' for int and 'c' for char
}

Example 4: c++ get data type

// Example
std::cout << "Data-type = " << typeid(YourVariable).name() << "\n";
  
// Syntax
typeid(YourVariable).name()

Tags:

Cpp Example