Integer Types

What are integer types?

Integers are numbers with no fractional part.

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

C++'s basic integer types, in order of increasing size, are called char, short, int and long.

Each comes in both signed and unsigned versions. Signed types can represent both negative and positive values. Unsigned types can't represent negative values.

Minimum Sizes

The various integer types vary across different computer systems. C++ offers a flexible standard with some guaranteed minimum sizes:

The sizeof operator

Use the sizeof operator to let you investigate type size with a program.

Symbolic Constants

Use the climits header file (or the older limits.h) to obtain the different limits of the different integer types.
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_MAX maximum unsigned int value
LONG_MAX maximum long value
LONG_MIN manimum long value
ULONG_MAX maximum unsigned long value

Declarations:

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

Initialization Rules

Initialization combines assignment with declaration. For instance, the statement

    int n_int = INT_MAX;

If you don't initialize a variable defined inside a function, the variable's value is undefined. That means the value is whatever happened to be sitting at that memory location prior to the creation of the variable.

Unsigned Types

Each of the three integer types comes in an unsigned variety that can't hold negative values.

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

To create unsigned versions of the basic integer types, 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

Note that unsigned by itself is short for unsigned int.

Integer Overflow

Integers behave much like an odometer or a VCR counter. If you go past the limit, the values just start over at the other end of the range.

C++ guarantees that unsigned types behave in this fashion.

However, C++ doesn't guarantee that integer types can exceed their limits (overflow and underflow) without complaint, but that is the most common behaviour on current implementations.

Integer Constants

An integer constant is one you write out explicitly, such as 212 or 1776. 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.

No matter how you write it, it's stored the same way in the computer: as a binary (base two) value.

C++ stores integer constants as type int unless there is a reason to do so otherwise. Two such reasons are:

Suffixes are letters placed at the end of a numeric constant to indicate the type.

An l or L suffix on an integer means the integer is a type long constant, a u or U suffix indicates an unsigned int constant, and ul (in any combination of orders and uppercase and lowercase) indicates a type unsigned long constant.