C++ Integer data type : built-in data type

By using integer data type we can make use of integer value in our program.Integer type may come in handy if your program requires integer value or an operation that involve integer value.To use the integer data type in our program we will use the keyword int and a variable should be declared for the type(if you don’t know what is variable please visit this link C++ Terms:Variables , Identifier , Keyword , Operators , Operands,literals).An example code below shows how to use the integer data type in a program.

int var=90 ;  //var is a variable of 'int' type

cout << var+var ; //Outputting the sum of the value

Output

180

 

There are many integral types in C++ and some can represent a small range of value while others can represent a larger range of value.Specifically there are four integer data types in C++:

i)Short int.
ii)int (plain int type).
iii)long int and
iv)long long.

 

An example below shows how to use the ‘short int‘ or just ‘short‘ type.

 

short short_int=89 ;

short_int+=short_int ;  //same as short_int=short_int + short_int ;

cout<< short_int ; 

Output,

178

 

Further division of integer type

The integer data type are divided into four types.But if we use the specifier ‘signed‘ and ‘unsigned‘ we can further classify various integer data type as:

i)short type : signed short int , unsigned short int.
ii)int type :signed int , unsigned int.
iii)long int :signed long int , unsigned long int.
iv)long long:signed long long , unsigned long long.
 

 

The various integral type occupy different sizes but their corresponding signed and unsigned type will occupy the same size.For instance,’signed int’ and ‘unsigned int’ will have the same size as ‘int’ type.The various type and their size is discuss in more detail below.


short int

This type has the smallest size among all the integral type.So,it can represent the smallest ranges of value.The table below give the meaning,integer size and range of values short int can represent.

 

Type Description short Integer
size(maximum)
Range of values
short int Can represent
both +ve and
-ve values.
2 bytes -32,768 to 32,767
Signed short int Same as short
int
2 bytes -32,768 to 32,767
Unsigned short int Can represent
only +ve
values.
2 bytes 0 to 65535

 


int type

This type is also known as plain int type.In every machine this type will have a minimum size same as that of short int type.The table below shows the meaning,size and range of values it can represent.

Type Description int type integer
size(Maximum)
Range of values
int Can represent
both +ve and
-ve values
4 bytes -2147483648 to
2147483647
Signed int Same as
int
4 bytes -2147483648 to
2147483647
Unsigned int Can represent
only +ve
values.
4 bytes 0 to 4294967295

 

Since int type has a size larger than short int it can represent larger range of values.


long int

As the name suggest this type can represent a longer integer values and it will have a minimum size(can be more) of int type.To use this type you can write just ‘long’ instead of ‘long int’.More information about this type is given below.

Type Description long integer Maximum
size(Maximum)
Range of values
long int Can represent
both +ve and
-ve values
8 bytes -9223372036854775808 to
9223372036854775807
Signed long int Same as long
int
8 bytes -9223372036854775808 to
9223372036854775807
Unsigned long int Can represent
only +ve
values.
8 bytes 0 to 18446744073709551615

 


long long

This type has the largest size among all the int type and so it can represent the largest and smallest range of integer values.A table consisting of it’s range and size is shown below.

Type Description Long long integer
size(Maximum)
Range of values
int Can represent
both +ve and
-ve values
8 bytes -9223372036854775808 to
9223372036854775807
Signed int Same as long
long int
8 bytes -9223372036854775808 to
9223372036854775807
Unsigned int Can represent
only +ve
values.
8 bytes 0 to 18446744073709551615

 

The program below shows the maximum and minimum values of different integer data type.Include the library <climits> because this library define the various minimum and maximum range of value each integer data type can represent.

#include <iostream>
#include <climits>

using namespace std;

int main( )
{
//Short min and max value
cout << “Short int minimum value ” << SHRT_MIN << endl ;
cout << “Short int maximum value ” << SHRT_MAX << endl ;
cout << “Unsigned short int maximum value ” << USHRT_MAx << endl ;

//Int min and max value
cout << “Int minimum value ” << INT_MIN << endl ;
cout << “Int maximum value ” << INT_MAX << endl ;
cout << “Unsigned int maximum value ” << UINT_MAx << endl ;

//Long int min and max value
cout << “Long int minimum value ” << LINT_MIN << endl ;
cout << “Long int maximum value ” << LINT_MAX << endl ;
cout << “Unsigned long int maximum value ” << ULINT_MAx << endl ;

//Long long min and max value
cout << “Long long minimum value ” << LLONG_MIN << endl ;
cout << “long long maximum value ” << LLONG_MAX << endl ;
cout << “Unsigned long long maximum value ” << ULLONG_MAx << endl ;

cin.get( ) ;
return 0 ;
}

Run the code and see the output yourself.

The minimum and maximum value given by the <climits> library for various signed and unsigned integer data type is the standard value in C++ and nothing can change it’s value unless the C++ committee intends to do so.




Leave a Reply

Your email address will not be published. Required fields are marked *