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++ 資料型別教程 中瞭解更多關於資料型別的資訊。