C++ 输出变量的类型 使用typeid().name()函数

#include<iostream>
#include<typeinfo>
#include<cstring>
using namespace std;

int main(){
	int i; 
    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;
}

输出结果:

其中 , i 表示int,b=bool c=char   s=short    l=long f=float, d=double  x=long long 

注意添加头文件#include<typeinfo>
在上头文件中定义了typeid()操作符可以输出变量的类型。

发布了182 篇原创文章 · 获赞 55 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/IT_flying625/article/details/103380022