Learn C++ - C++ Integer Types






Integers are numbers with no fractional part, such as 2, 98, ?5286, and 0.

The various C++ integer types differ in the amount of memory they use to hold an integer.

Some types (signed types) can represent both positive and negative values, whereas others (unsigned types) can't represent negative values.

The usual term for describing the amount of memory used for an integer is width.

C++'s basic integer types, in order of increasing width, are char, short, int, long, and, with C++11, long long.

Each comes in both signed and unsigned versions.

That gives you a choice of ten different integer types!

The short, int, long, and long long Integer Types

By using different numbers of bits to store values, the C++ types short, int, long, and long long can represent up to four different integer widths.

A short integer is at least 16 bits wide.

An int integer is at least as big as short.

A long integer is at least 32 bits wide and at least as big as int.

A long long integer is at least 64 bits wide and at least as big as long.

You use these type names to declare variables just as you would use int:

short score;             // creates a type short integer variable 
int temperature;         // creates a type int integer variable 
long position;           // creates a type long integer variable 

If you want to know how your system's integers size up, you can use C++ tools to investigate type sizes with a program.

sizeof operator returns the size, in bytes, of a type or a variable.


// Writing values of variables to cout 
#include <iostream> 
  /*ww w  .  ja  v a  2s. c o m*/
int main() 
{ 
  int apple_count {15};                            // Number of apples 
  int orange_count {5};                            // Number of oranges 
  int total_fruit {apple_count + orange_count};    // Total number of fruit 
  
  std::cout << "The value of apple_count is "  << apple_count  << std::endl; 
  std::cout << "The value of orange_count is " << orange_count << std::endl; 
  std::cout << "The value of total_fruit is "  << total_fruit  << std::endl; 
} 

The code above generates the following result.





Signed Integer Types

The following table shows the complete set of fundamental types that store signed integers - that is both positive and negative values.

The memory allocated for each type, and hence the range of values it can store, may vary between different compilers.

Type NameTypical Size (bytes)Range of Values
signed char1-128 to 127
short/short int2-256 to 255
int4-2,147,483,648 to +2,147,483,647
long/long int4-2,147,483,648 to +2,147,483,647
long long/long long int8-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807




Example

The following code shows how to get some integer limits.


#include <iostream> 
#include <climits>              // use limits.h for older systems 
int main() { 
     using namespace std; 
     int n_int = INT_MAX;        // initialize n_int to max int value 
     short n_short = SHRT_MAX;   // symbols defined in climits file 
     long n_long = LONG_MAX; 
     long long n_llong = LLONG_MAX; 
// w ww.ja  v  a2 s  .  c o  m
     // sizeof operator yields size of type or of variable 
     cout << "int is " << sizeof (int) << " bytes." << endl; 
     cout << "short is " << sizeof n_short << " bytes." << endl; 
     cout << "long is " << sizeof n_long << " bytes." << endl; 
     cout << "long long is " << sizeof n_llong << " bytes." << endl; 
     cout << endl; 

     cout << "Maximum values:" << endl; 
     cout << "int: " << n_int << endl; 
     cout << "short: " << n_short << endl; 
     cout << "long: " << n_long << endl; 
     cout << "long long: " << n_llong << endl << endl; 

     cout << "Minimum int value = " << INT_MIN << endl; 
     cout << "Bits per byte = " << CHAR_BIT << endl; 
     return 0; 
} 

The code above generates the following result.

Note

The following table lists Symbolic Constants from climits.

Symbolic ConstantRepresents
CHAR_BITNumber of bits in a char
CHAR_MAXMaximum char value
CHAR_MINMinimum char value
SCHAR_MAXMaximum signed char value
SCHAR_MINMinimum signed char value
UCHAR_MAXMaximum unsigned char value
SHRT_MAXMaximum short value
SHRT_MINMinimum short value
USHRT_MAXMaximum unsigned short value
INT_MAXMaximum int value
INT_MINMinimum int value
UINT_MAXMaximum unsigned int value
LONG_MAXMaximum long value
LONG_MINMinimum long value
ULONG_MAXMaximum unsigned long value
LLONG_MAXMaximum long long value
LLONG_MINMinimum long long value
ULLONG_MAXMaximum unsigned long long value

Initialization

Initialization combines assignment with declaration.

For example, the following statement declares the n_int variable and sets it to the largest possible type int value:

int n_int = INT_MAX;

You can also use literal constants, such as 255, to initialize values.

You can initialize a variable to an expression, provided that all the values in the expression are known when program execution reaches the declaration:

int uncles = 5;                       // initialize uncles to 5 
int aunts = uncles;                   // initialize aunts to 5 
int chairs = aunts + uncles + 4;      // initialize chairs to 14 

Unsigned Types

Each of the four integer types you learned has an unsigned variety that can't hold negative values.

This has the advantage of increasing the largest value the variable can hold.

If short represents the range ?32,768 to +32,767, the unsigned version can represent the range 0 to 65,535.

You should use unsigned types for values that are never negative, such as populations.

To create unsigned versions of the basic integer types, you just use the keyword unsigned to modify the declarations:

unsigned short change;          // unsigned short type 
unsigned int rovert;            // unsigned int type 
unsigned quarterback;           // also unsigned int 
unsigned long gone;             // unsigned long type 
unsigned long long lang_lang;   // unsigned long long type 

unsigned is short for unsigned int.

The following code illustrates the use of unsigned types.

It also shows what might happen if your program tries to go beyond the limits for integer types.


#include <iostream> 
#include <climits>  // defines INT_MAX as largest int value 
#define ZERO 0      // makes ZERO symbol for 0 value 
/*www .  j a va 2s.c  o  m*/
int main() 
{ 
     using namespace std; 
     short s = SHRT_MAX;     // initialize a variable to max value 
     unsigned short sue = s;

     cout << "s has " << s << " dollars and Sue has " << sue; 
     cout << "Add $1 to each account." << endl << "Now "; 
     s = s + 1; 
     sue = sue + 1; 
     cout << "s has " << s << " dollars and Sue has " << sue; 
     s = ZERO; 
     sue = ZERO; 
     cout << "s has " << s << " dollars and Sue has " << sue; 
     s = s - 1; 
     sue = sue - 1; 
     cout << "s has " << s << " dollars and Sue has " << sue; 
     cout << "Hi!" << endl; 
     return 0; 
} 

The code above generates the following result.

Integer Literals

An integer literal, or constant, is one you write out explicitly, such as 212 or 17.

C++, like C, lets you write integers in three different number bases: base 10, base 8, and base 16.

C++ uses the first digit or two to identify the base of a number constant.

If the first digit is in the range 1?9, the number is base 10 (decimal); thus 93 is base 10.

If the first digit is 0 and the second digit is in the range 1?7, the number is base 8 (octal); thus 042 is octal and equal to 34 decimal.

If the first two characters are 0x or 0X, the number is base 16 (hexadecimal); thus 0x42 is hex and equal to 66 decimal.

For hexadecimal values, the characters a?f and A?F represent the hexadecimal digits corresponding to the values 10?15.

Here are some more examples of hexadecimal literals:

Hexadecimal literals:0x1AF 0x123U 0xAL  0xcad 0xFF 
Decimal literals:    431     291U  10L   3245  255 

Here are some examples for Octal literals.

Octal literals:       0657     0443U      012L     06255      0377 
Decimal literals:      431      291U       10L       3245      255 

0xF is 15 and 0xA5 is 165 (10 sixteens plus 5 ones). The following code shows hex and octal literals.


#include <iostream> 
int main() /*  w  w w.  j a v a  2  s .  co  m*/
{ 
    using namespace std; 
    int my_decimal = 42;     // decimal integer literal 
    int my_hexi = 0x42;      // hexadecimal integer literal 
    int my_oct = 042;        // octal integer literal 

    cout << "my_decimal = " << my_decimal << " (42 in decimal)\n"; 
    cout << "my_hexi = " << my_hexi << " (0x42 in hex)\n"; 
    cout << "my_oct = " << my_oct << " (042 in octal)\n"; 
    return 0; 
} 

The code above generates the following result.

Binary Literals

You write a binary integer literal as a sequence of binary digits (0 or 1) prefixed by 0b or 0B.

A binary literal can have L or LL as a suffix to indicate it is type long or long long, and u or U if it is an unsigned literal.

For example:

Binary literals: 0B110101111  0b100100011U 0b1010L  0B11001101 0b11111111 
Decimal literals:        431          291U     10L        3245         255 

Example 2

The following code shows how to display values in hex and octal.


#include <iostream> 
using namespace std; 
int main() /*from ww w  .  j  av a  2s.c o m*/
{ 
     int my_decimal = 42; 
     int my_hexi = 42; 
     int my_oct = 42; 

     cout << "my_decimal = " << my_decimal << " (decimal for 42)" << endl; 
     cout << hex;      // manipulator for changing number base 
     cout << "my_hexi = " << my_hexi << " (hexadecimal for 42)" << endl; 
     cout << oct;      // manipulator for changing number base 
     cout << "my_oct = " << my_oct << " (octal for 42)" << endl; 
     return 0; 
} 

The code above generates the following result.

Example 3

Converting distances


#include <iostream>                    // For output to the screen 
int main() 
{ /*from ww w .j  a va  2 s . c om*/
  unsigned int yards =10, feet =10, inches =10; 
   
  const unsigned int feet_per_yard {3U}; 
  const unsigned int inches_per_foot {12U}; 
  unsigned int total_inches {}; 
  total_inches = inches + inches_per_foot*(yards*feet_per_yard + feet); 
  std::cout << "The distances corresponds to " << total_inches << " inches.\n"; 
  
  std::cout << "Enter a distance in inches: "; 
  std::cin >> total_inches; 
  feet = total_inches/inches_per_foot; 
  inches = total_inches%inches_per_foot; 
  yards = feet/feet_per_yard; 
  feet = feet%feet_per_yard; 
  std::cout << "The distances corresponds to " 
            << yards  << " yards " 
            << feet   << " feet " 
            << inches << " inches." << std::endl; 
} 

The code above generates the following result.