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


integer data type and various integer size

By using integral or 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 f int type

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

Like the int data type 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 ;

 
The integer data type are divided into four types but using 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 signed and unsigned type will occupy the same size as the type from which it is derived.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.


 


Earn money($$$) from your website-Join Now!

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.
 

TypeDescriptionshort Integer
size(maximum)
Range of values
short intCan represent
both +ve and
-ve values.
2 bytes-32,768 to 32,767
Signed short intSame as short
int
2 bytes-32,768 to 32,767
Unsigned short intCan represent
only +ve
values.
2 bytes0 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.

TypeDescriptionint type integer
size(Maximum)
Range of values
intCan represent
both +ve and
-ve values
4 bytes-2147483648 to
2147483647
Signed intSame as
int
4 bytes-2147483648 to
2147483647
Unsigned intCan represent
only +ve
values.
4 bytes0 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.

TypeDescriptionlong integer Maximum
size(Maximum)
Range of values
long intCan represent
both +ve and
-ve values
8 bytes-9223372036854775808 to
9223372036854775807
Signed long intSame as long
int
8 bytes-9223372036854775808 to
9223372036854775807
Unsigned long intCan represent
only +ve
values.
8 bytes0 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.

TypeDescriptionLong long integer
size(Maximum)
Range of values
intCan represent
both +ve and
-ve values
8 bytes-9223372036854775808 to
9223372036854775807
Signed intSame as long
long int
8 bytes-9223372036854775808 to
9223372036854775807
Unsigned intCan represent
only +ve
values.
8 bytes0 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.

//Size of different int types

#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 ;
}

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 unless the C++ committee intends to do so;to know the difference between signed and unsigned int type visit the link provided below.


Related Link

->Differences between signed int and unsigned int type.
 
->Negative value assigned to unsigned int variable.