获取C变量类型直接上代码1234567891011121314151617181920212223242526272829303132333435363738394041#include type_traits#include typeinfo#include memory#include string#include cstdlib#include iostream#ifndef _MSC_VER#include cxxabi.h#endifusingnamespacestd;templateclassTstring type_name(){typedeftypenameremove_referenceT::type TR;unique_ptrchar,void(*)(void*) own(#ifndef _MSC_VERabi::__cxa_demangle(typeid(TR).name(), nullptr, nullptr, nullptr),#elsenullptr,#endiffree);string r own ! nullptr ? own.get() :typeid(TR).name();if(is_constTR::value)r const;if(is_volatileTR::value)r volatile;if(is_lvalue_referenceT::value)r ;elseif(is_rvalue_referenceT::value)r ;returnr;}intmain(){inta 1;constintb a;cout type_namedecltype(b)() endl;}定义了一个模板函数type_name()可以对传入的模板类型T进行类型判断结合指针、左值/右值引用、常类型准确得出变量的类型。在调用该函数时使用了decltype关键字传入待确定的变量然后输出变量类型。以上运行的结果为int const如果要输出int指针的类型123456intmain(){inta 1;int* b a;cout type_namedecltype(b)() endl;}结果为int*可以看出该函数得出的结果是非常准确而又精简的。与传统方法的对比传统输出C类型的方法是使用typeid(var).name()其弊端在于输出的结果不完整或者过于冗长。例如1. 传统方法打印int类型1234567#include iostreamintmain(){inta 10;std::cout typeid(a).name() std::endl;}运行结果为i只得到了int的缩写i。2. 传统方法打印string类型12345678#include iostream#include stringintmain(){std::string a abc;std::cout typeid(a).name() std::endl;}运行结果为NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE过于冗长不便于看出类型名称。3. 传统方法打印引用类型12345678#include iostreamintmain(){inta0 23;int a a0;std::cout typeid(a).name() std::endl;}运行结果为i并没有准确地输出我们想要的结果。总结使用了稍微复杂的模板函数可以准确输出C的变量类型而非大多数人提到的type_id(var).name()打印出来的结果有各种不恰当的地方。获取C数据类型取值范围包含头文件12#include limits#include float.h类型变量定义12345678910111213inta 2;//32位整数 4字节 4byte 32bitunsignedintb 2u;//无符号32位整数 4字节 4byte 32bitsignedintb1 -2147483648;//有符号32位整数 4字节 4byte 32bitlongc 2l;//32位整数 4字节 4byte 32bitunsignedlongd 2ul;//无符号32位整数 4字节 4byte 32bitdoublee 2.0;//双精度浮点数floatf 2.0f;//单精度浮点数longdoubleg2.0l;//长双精度浮点数longlongh2ll;//超长整数 64位整数 8字节 8byte 64bitshorti 2;//16位整数 2字节 2byte 16bitunsignedshorti1 2;//无符号16位整数 2字节 2byte 16bitcharj 2;//字符类型 1字节 1byte 8bitcharj1 2;//无符号字符类型 1字节 1byte 8bit取类型值范围12345678910111213std::cout int a is : a int类型取值范围:INT_MIN ,INT_MAX \nsigned int b1 is : b1 int类型取值范围: INT_MIN ,INT_MAX \nunsigned int b is : b unsigned int类型取值范围: 0 ,UINT_MAX \nlong c is : c long类型取值范围: LONG_MIN ,LONG_MAX \nunsigned long d is : d unsigned long类型取值范围: 0 ,ULONG_MAX \ndouble e is : e double类型取值范围:DBL_MIN ,DBL_MAX \nfloat f is : f float类型取值范围:FLT_MIN ,FLT_MAX \nlong double g is : g long double类型取值范围:LDBL_MIN ,LDBL_MAX \nlong long h is : h long long类型取值范围: LLONG_MIN ,LLONG_MAX \nshort i is : i short类型取值范围: SHRT_MIN ,SHRT_MAX \nunsigned short i is : i1 unsigned short类型取值范围: 0 ,USHRT_MAX \nchar j is : j char类型取值范围: CHAR_MIN ,CHAR_MAX \nunsigned char j1 is : j1 unsigned char类型取值范围: 0 ,UCHAR_MAX \n;输出效果int a is :2 int类型取值范围:-2147483648,2147483647signed int b1 is :-2147483648 int类型取值范围: -2147483648,2147483647unsigned int b is : 2 unsigned int类型取值范围: 0,4294967295long c is : 2 long类型取值范围: -9223372036854775808,9223372036854775807unsigned long d is : 2 unsigned long类型取值范围: 0,18446744073709551615double e is : 2 double类型取值范围:2.22507e-308,1.79769e308float f is : 2 float类型取值范围:1.17549e-38,3.40282e38long double g is : 2 long double类型取值范围:3.3621e-4932,1.18973e4932long long h is : 2 long long类型取值范围: -9223372036854775808,9223372036854775807short i is : 2 short类型取值范围: -32768,32767unsigned short i is : 2 unsigned short类型取值范围: 0,65535char j is : 2 char类型取值范围: -128,127unsigned char j1 is : 2 unsigned char类型取值范围: 0,255完整代码123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354#include iostream#include chrono#include thread#include limits#include float.hintmain() {inta 2;//32位整数 4字节 4byte 32bitunsignedintb 2u;//无符号32位整数 4字节 4byte 32bitsignedintb1 -2147483648;//有符号32位整数 4字节 4byte 32bitlongc 2l;//32位整数 4字节 4byte 32bitunsignedlongd 2ul;//无符号32位整数 4字节 4byte 32bitdoublee 2.0;//双精度浮点数floatf 2.0f;//单精度浮点数longdoubleg2.0l;//长双精度浮点数longlongh2ll;//超长整数 64位整数 8字节 8byte 64bitshorti 2;//16位整数 2字节 2byte 16bitunsignedshorti1 2;//无符号16位整数 2字节 2byte 16bitcharj 2;//字符类型 1字节 1byte 8bitcharj1 2;//无符号字符类型 1字节 1byte 8bit//进度表示写法intbin2 0b11111111;//二进制 0和1 前缀:0b stdc 14intbin8 077;//八进制 0~7 前缀:0intbin16 0xff;//十六进制 0~F 前缀:0xintaa INT_MAX * 2 1;unsignedintbb UINT_MAX;if(aa bb){std::coutUINT_MAXstd::endl;}std::cout int a is : a int类型取值范围:INT_MIN ,INT_MAX \nsigned int b1 is : b1 int类型取值范围: INT_MIN ,INT_MAX \nunsigned int b is : b unsigned int类型取值范围: 0 ,UINT_MAX \nlong c is : c long类型取值范围: LONG_MIN ,LONG_MAX \nunsigned long d is : d unsigned long类型取值范围: 0 ,ULONG_MAX \ndouble e is : e double类型取值范围:DBL_MIN ,DBL_MAX \nfloat f is : f float类型取值范围:FLT_MIN ,FLT_MAX \nlong double g is : g long double类型取值范围:LDBL_MIN ,LDBL_MAX \nlong long h is : h long long类型取值范围: LLONG_MIN ,LLONG_MAX \nshort i is : i short类型取值范围: SHRT_MIN ,SHRT_MAX \nunsigned short i is : i1 unsigned short类型取值范围: 0 ,USHRT_MAX \nchar j is : j char类型取值范围: CHAR_MIN ,CHAR_MAX \nunsigned char j1 is : j1 unsigned char类型取值范围: 0 ,UCHAR_MAX \n;std::cout二进制0b11111111值是:bin2std::endl;std::cout八进制077值是:bin8std::endl;std::cout十六进制0xff值是:bin16std::endl;std::cout 等待5秒后退出程序std::endl;std::this_thread::sleep_for(std::chrono::seconds(5));return0;}复制讲解以上为个人经验希望能给大家一个参考