blog icon indicating copy to clipboard operation
blog copied to clipboard

C++: Check data type

Open qingquan-li opened this issue 2 years ago • 0 comments

Reference:

  • https://stackoverflow.com/questions/11310898/how-do-i-get-the-type-of-a-variable

#include <iostream>
#include <typeinfo>

using namespace std;

int main() {
    int an_int = 1;
    cout << typeid(an_int).name() << endl;         // i
    cout << typeid(-2).name() << endl;             // i
    cout << typeid(3.14).name() << endl;           // d
    cout << typeid('a').name() << endl;            // c
    cout << typeid("Hi").name() << endl;           // A3_c
    cout << typeid("Hello World!").name() << endl; // A13_c
    cout << typeid(true).name() << endl;           // b
    return 0;
}

qingquan-li avatar Jun 08 '22 17:06 qingquan-li