By Stephen R. Davis

C++ allows you to expand on integer variable types by adding the following descriptors on the front: const, unsigned, short, or else long. Thus you could declare something like the following:

unsigned long int ulnVariable;

A const variable cannot be modified. All numbers are implicitly const. Thus, 3 is of type const int, while 3.0 is a const double, and ‘3’ is a const char.

An unsigned variable can take on non-negative values only; however, it can handle a number roughly twice as large as its signed sibling. Thus an unsigned int has a range of 0 to 4 billion (as opposed to the regular signedint‘s range of −2 billion to 2 billion).

C++ allows you to declare a short int and a long int. For example, a short int takes less space but has a more limited range than a regular int, whereas a long int takes more storage and has a significantly larger range.

The int is assumed. Thus the following two declarations are both accepted and completely equivalent:

long int lnVar1;  // declare a long int
long lnVar2;      // also a long int; int is assumed

The C++ 2011 Standard even defines a long long int and a long double. These are just like long int and double, respectively, only more so — more accuracy and larger range.

Not all combinations are allowed. For example, unsigned can be applied only to the counting types int and char. The following table shows the legal combinations and their meaning, along with how to declare a constant of that type.

The Common C++ Variable Types
Type Declaring a Constant What It Is
int 1 A simple counting number, either positive or negative.
unsigned int 1U A non-negative counting number.
short int A potentially smaller version of the int. It uses less memory but has a more limited
range.
long int 1L A potentially larger version of the int. It may use more memory but has a larger
range.
long long int 1LL A version of the int that is
potentially even larger.
float 1.0F A single-precision real number.
double 1.0 A double-precision real number.
long double A potentially larger floating-point number. On the PC, long double is the native size for numbers
internal to the numeric processor.
char ‘c’ A single char variable stores a
single character. Not suitable for arithmetic.
wchar_t L’c’ A wide character. Used to store larger character sets such as
Chinese ideograms and Japanese kanji symbols. Also known as UTF or
Unicode.

How far do numbers range?

It may seem odd, but the C++ standard doesn’t actually say exactly how big a number each data type can accommodate. The standard addresses only the relative size of each variable type. For example, it says that the maximum long int is at least as large as the maximum int.

The 2011 standard says a little bit more than that — for example, a long int must be at least 32 bits — but it still doesn’t specify the size of every variable type.

The authors of C++ weren’t trying to be mysterious. They wanted to allow the compiler to implement the absolute fastest code possible for the base machine. The standard was designed to work for all different types of processors, running different operating systems.

In fact, the standard size of an int has changed over the past decades. Before 2000, the standard int on most PCs was 2 bytes and had a range of plus or minus 64,000. Sometime around 2000, the basic word size on the Intel processors changed to 32 bits. Most compilers changed to the default int of today — it’s 4 bytes and has a range of plus or minus 2 billion.

The following table provides the size and range of each variable type on most other compilers meant for an Intel processor running on a 32-bit operating system.

Range of Numeric Types
Type Size [bytes] Accuracy Range
short int 2 exact −32,768 to 32,767
int 4 exact −2,147,483,648 to 2,147,483,647
long int 4 exact −2,147,483,648 to 2,147,483,647
long long int 8 exact −9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
float 4 7 digits +/− 3.4028 * 10+/-38
double 8 16 digits +/− 1.7977 * 10+/-308
long double 12 19 digits +/−1.1897 * 10+/-4932

Attempting to calculate a number that is beyond the range of a variable’s type is known as an overflow. The C++ standard generally leaves the results of an overflow undefined. That’s another way that the inventors of C++ wanted to leave the language flexible so that the machine code generated would be as fast as possible.

On the PC, a floating-point overflow generates an exception that, if not handled, will cause your program to crash. As bad as that sounds, an integer overflow is even worse — C++ generates an incorrect result without complaint.