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.
The various integer types vary across different computer systems. C++ offers a flexible standard with some guaranteed minimum sizes:
short
integer is at least 16 bits.int
integer is at least as big as short.long
integer is at least 32 bits AND at least as big as
int
.sizeof
operator
Use the sizeof
operator to let you investigate type size with a program.
sizeof
operator with a type name, such as
int
, you enclose the name in parentheses. sizeof(int)
sizeof n_short
Use the climits
header file (or the older limits.h
) to
obtain the different limits of the different integer types.
Symbolic Constant | Represents |
---|---|
CHAR_BIT | number of bits in a char |
CHAR_MAX | maximum char value |
CHAR_MIN | minimum char value |
SCHAR_MAX | maximum signed char value |
SCHAR_MIN | minimum signed char value |
UCHAR_MAX | maximum unsigned char value |
SHRT_MAX | maximum short value |
SHRT_MIN | minimum short value |
USHRT_MAX | maximum unsigned short value |
INT_MAX | maximum int value |
INT_MIN | minimum 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 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.
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.
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.
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:
int
.
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.