C++ int Keyword
Definition and Usage
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.
With 16 bits it can store positive and negative numbers with values between -32768 and 32767, or between 0 and 65535 when unsigned.
With 32 bits it can store positive and negative numbers with values between -2147483648 and 2147483647, or between 0 and 4294967295 when unsigned.
Modifiers
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.
More Examples
Example
Create signed, unsigned, short and long integers: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";
Related Pages
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.
Read more about data types in our C++ Data Types Tutorial.