Qingquan-Li / blog

My Blog
https://Qingquan-Li.github.io/blog/
132 stars 16 forks source link

C++: Check data type #190

Open Qingquan-Li opened 2 years ago

Qingquan-Li commented 2 years ago

Reference:


#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;
}