C++获取变量类型并输出

头文件:

#include<typeinfo>

使用

typeid(data).name()
#include<iostream>
#include<typeinfo>
using namespace std;

int main(){
    bool a;
    char b;
    short c;
    int d;
    long e;
    float f;
    double g;
    long long h;
    cout<<typeid(i).name()<<endl;
    cout<<typeid(a).name()<<endl;
    cout<<typeid(b).name()<<endl;
    cout<<typeid(c).name()<<endl;
    cout<<typeid(d).name()<<endl;
    cout<<typeid(e).name()<<endl;
    cout<<typeid(f).name()<<endl;
    cout<<typeid(g).name()<<endl;
    cout<<typeid(h).name()<<endl;
    return 0;
}

out
gcc为简写,VS为全称

bool:                     b
 
char:                     c
signed char:              a
unsigned char:            h
 
(signed) short (int):     s
unsigned short (int):     t
 
(signed) (int):           i
unsigned (int):           j
 
(signed) long (int):      l
unsigned long (int):      m
 
(signed) long long (int): x
unsigned long long (int): y
 
float:                    f
double:                   d
long double:              e
发布了415 篇原创文章 · 获赞 251 · 访问量 68万+

猜你喜欢

转载自blog.csdn.net/qq_35608277/article/details/104628419