C++ 基本数据类型

C++有七种内置的数据类型:

  1. bool
  2. char
  3. int
  4. float
  5. double
  6. wchar_t(宽字符型):如果常量以 L(仅当大写时)开头,则表示它是一个宽字符常量(例如 L’x’),此时它必须存储在 wchar_t 类型的变量中。否则,它就是一个窄字符常量(例如 ‘x’),此时它可以存储在 char 类型的简单变量中。
  7. void
#include 
#include 
using namespace std;
int main(){
        cout << "bool:\t\t\t" << sizeof(bool) << "\t字节\t" << "范围:" << numeric_limits<bool>::min() << "~" << numeric_limits<bool>::max() << endl;
        cout << "char:\t\t\t" << sizeof(char) << "\t字节\t" << "范围:" << (int)numeric_limits<char>::min() << "~" << (int)numeric_limits<char>::max() << endl;
        cout << "signed char:\t\t" << sizeof(signed char) << "\t字节\t" << "范围:" << (int)numeric_limits<signed char>::min()<<"~"<< (int)numeric_limits<signed char>::max() << endl;
        cout << "unsigned char:\t\t" << sizeof(unsigned char) << "\t字节\t" << "范围:" << (int)numeric_limits<unsigned char>::min() << "~" << (int)numeric_limits<unsigned char>::max() << endl;
        cout << "int:\t\t\t" << sizeof(int) << "\t字节\t" << "范围:" << numeric_limits<int>::min() << "~" << numeric_limits<int>::max() << endl;
        cout << "signed int:\t\t" << sizeof(signed int) << "\t字节\t" << "范围:" << numeric_limits<signed int>::min() << "~" << numeric_limits<signed int>::max() << endl;
        cout << "signed:\t\t\t" << sizeof(signed) << "\t字节\t" << "范围:" << numeric_limits<signed>::min() << "~" << numeric_limits<signed>::max() << endl;
        cout << "unsigned int:\t\t" << sizeof(unsigned int) << "\t字节\t" << "范围:" << numeric_limits<unsigned int>::min() << "~" << numeric_limits<unsigned int>::max() << endl;
        cout << "unsigned:\t\t" << sizeof(unsigned) << "\t字节\t" << "范围:" << numeric_limits<unsigned>::min() << "~" << numeric_limits<unsigned>::max() << endl;
        cout << "short int:\t\t" << sizeof(short int) << "\t字节\t" << "范围:" << numeric_limits<short int>::min() << "~" << numeric_limits<short int>::max() << endl;
        cout << "short:\t\t\t" << sizeof(short) << "\t字节\t" << "范围:" << numeric_limits<short>::min() << "~" << numeric_limits<short>::max() << endl;
        cout << "signed short int:\t" << sizeof(signed short int) << "\t字节\t" << "范围:" << numeric_limits<signed short int>::min() << "~" << numeric_limits<signed short int>::max() << endl;
        cout << "unsigned short int:\t" << sizeof(unsigned short int) << "\t字节\t" << "范围:" << numeric_limits<unsigned short int>::min() << "~" << numeric_limits<unsigned short int>::max() << endl;
        cout << "long int:\t\t" << sizeof(long int) << "\t字节\t" << "范围:" << numeric_limits<long int>::min() << "~" << numeric_limits<long int>::max() << endl;
        cout << "long:\t\t\t" << sizeof(long) << "\t字节\t" << "范围:" << numeric_limits<long>::min() << "~" << numeric_limits<long>::max() << endl;
        cout << "signed long int:\t" << sizeof(signed long int) << "\t字节\t" << "范围:" << numeric_limits<signed long int>::min() << "~" << numeric_limits<unsigned long int>::max() << endl;
        cout << "unsigned long int:\t" << sizeof(unsigned long int) << "\t字节\t" << "范围:" << numeric_limits<unsigned long int>::min() << "~" << numeric_limits<unsigned long int>::max() << endl;
        cout << "float:\t\t\t" << sizeof(float) << "\t字节\t" << "范围:" << numeric_limits<float>::min() << "~" << numeric_limits<float>::max() << endl;
        cout << "double:\t\t\t" << sizeof(double) << "\t字节\t"   << "范围:" << numeric_limits<double>::min() << "~" << numeric_limits<double>::max() << endl;
        cout << "long double:\t\t" << sizeof(long double) << "\t字节\t" << "范围:" << numeric_limits<long double>::min() << "~" << numeric_limits<long double>::max() << endl;
        cout << "wchar_t:\t\t" << sizeof(wchar_t) << "\t字节\t" << "范围:" << numeric_limits<wchar_t>::min() << "~" << numeric_limits<wchar_t>::max() << endl;
		return 0;
}

输出结果如下:

~/Desktop/c++$ ./example01
bool:				1	字节	范围:0~1
char:				1	字节	范围:-128~127
signed char:		1	字节	范围:-128~127
unsigned char:		1	字节	范围:0~255
int:				4	字节	范围:-2147483648~2147483647
signed int:			4	字节	范围:-2147483648~2147483647
signed:				4	字节	范围:-2147483648~2147483647
unsigned int:		4	字节	范围:0~4294967295
unsigned:			4	字节	范围:0~4294967295
short int:			2	字节	范围:-32768~32767
short:				2	字节	范围:-32768~32767
signed short int:	2	字节	范围:-32768~32767
unsigned short int:	2	字节	范围:0~65535
long int:			8	字节	范围:-9223372036854775808~9223372036854775807
long:				8	字节	范围:-9223372036854775808~9223372036854775807
signed long int:	8	字节	范围:-9223372036854775808~18446744073709551615
unsigned long int:	8	字节	范围:0~18446744073709551615
float:				4	字节	范围:1.17549e-38~3.40282e+38
double:				8	字节	范围:2.22507e-308~1.79769e+308
long double:		16	字节	范围:3.3621e-4932~1.18973e+4932
wchar_t:			4	字节	范围:-2147483648~2147483647

各类型的变量所能存储的最大值和最小值如上所示。
类型还可以用以下修饰符进行修饰:

  • signed(有符号,就是分正负,整型类型默认都是这个)
  • unsigned
  • short
  • long

修饰符 signed、unsigned、long 和 short 可应用于intsigned 和 unsigned 可应用于char,long 可应用于double
C++ 允许使用速记符号来声明无符号短整数或无符号长整数,即可以不写 int,只写单词 unsigned、short 或 unsigned、long,int 是隐含的,如:

short i = 10; // 等价于 short int
unsigned a = 100; // 等价于 unsigned int

C++中的变量还可以用类型限定符来修饰,用来提供了变量的额外信息:

  • const:const 类型的对象在程序执行期间不能被修改改变。
  • volatile: 告诉编译器,变量的值可能以程序未明确指定的方式被改变。
  • restrict:由 restrict 修饰的指针是唯一一种访问它所指向的对象的方式。只有 C99 增加了新的类型限定符 restrict。

你可能感兴趣的:(C/C++,c++)