C++ int 关键字
定义和用法
The int
keyword is a data type that is usually 32 bits long which stores whole numbers. Most implementations will give the int
type 32 bits, but some only give it 16 bits.
使用 16 位,它可以存储介于 -32768 和 32767 之间的正负数,或者在无符号时存储 0 到 65535 之间的数。
使用 32 位时,它可以存储值介于 -2147483648 和 2147483647 之间的正数和负数,或者在无符号时存储介于 0 和 4294967295 之间的正数。
修饰符
The size of the int
can be modified with the short
and long
modifiers.
The short
keyword ensures a maximum of 16 bits.
The long
keyword ensures at least 32 bits but may extend it to 64 bits. long long
ensures at least 64 bits.
64 bits can store positive and negative numbers with values between -9223372036854775808 and 9223372036854775807, or between 0 and 18446744073709551615 when unsigned.
更多示例
示例
创建有符号、无符号、短整型和长整型int myInt = 4294967292;
unsigned int myUInt = 4294967292;
short int mySInt = 65532;
unsigned short int myUSInt = 65532;
long int myLInt = 18446744073709551612;
unsigned long int myULInt = 18446744073709551612;
cout << " size: " << 8*sizeof(myInt) << " bits value: " << myInt << "\n";
cout << " size: " << 8*sizeof(myUInt) << " bits value: " << myUInt << "\n";
cout << " size: " << 8*sizeof(mySInt) << " bits value: " << mySInt << "\n";
cout << " size: " << 8*sizeof(myUSInt) << " bits value: " << myUSInt << "\n";
cout << " size: " << 8*sizeof(myLInt) << " bits value: " << myLInt << "\n";
cout << " size: " << 8*sizeof(myULInt) << " bits value: " << myULInt << "\n";
相关页面
The unsigned
keyword can allow an int
to represent larger positive numbers by not representing negative numbers.
The short
keyword ensures that an int
has 16 bits.
The long
keyword ensures that an int
has at least 32 bits.
在我们的 C++ 数据类型教程 中了解更多关于数据类型的信息。