CSE 202: Computer Science II, Winter 2018
Fundamental types

The fundamental types are the types that are built into the C++ language. To declare a variable of a fundamental type does not require the inclusion of any header file. A few fundamental types you should already know are:

All other types within C++ are described or constructed in terms of the fundamental types.

Integer types

The int type can be modified by using certain keywords right before it. This effectively modifies the type.

Signedness

The signedness of an integer determines whether or not it can hold a negative value. By default, all integer types are signed which means they can represent a positive or negative value. Using the keyword unsigned before int disallows negative values for this type and consequently, it can store a value that lies within a broader range of positive values.

Size

The size of an integer can be affected by using the keywords short or long before int. An integer's size affects how much space it occupies in the computer's memory; integers that require more storage can store a value that lies within a broader range of values.

Type Size
short int At least 16 bits
int At least 16 bits, but typically 32 bits
long int At least 32 bits

The unsigned integers occupy the same amount of storage as their signed equivalents

You can mix integer modifiers together in any order. For example, unsigned long int is an itneger occupying at least 32 bits and can only store a positive value.

Both the size and signedness of an integer affects the range of values it can take on.

Signedness Size Value range
signed 16 bits ±32767
unsigned 16 bits 0 to 65535
signed 32 bits ±2.15 × 109
unsigned 32 bits 0 to 4.29 × 109
Integer literals

An integer value expressed directly is known as an integer literal. The expression 500 is an example of an integer literal.

Integer literals can further be categorized into decimal literals, octal literals, and hex literals.

Decimal literals are expressed in base 10, octal literals are expressed in base 8, and hex literals are expressed in base 16. The values 20, 024 and 0x14 are all equivalent integer values expressed in decimal, octal, and hexadecimal respectively.

C++ requires that you have a leading zero in your integer value to indicate an octal literal; for a hex literal, a leading "0x" is required, otherwise decimal is assumed.

Integer literals have a default width of that for a regular int, but a suffix may be added to describe integer literals for unsigned int, long int and unsigned long int.

Integer literal suffix Type
int
u unsigned int
l long int
ul unsigned long int

When different types of integers are used in the same expression, then implicit conversion takes place (which will be discussed in a later article).

Floating-point types

The following types are floating-point types:

All of these types are used for representing real numbers to a certain accuracy. The difference between these types are the range of values they can take on.

The C++ standard requires that float be able to take on a subset of values which double can take on, and that double be able to take on a subset of values which long double can take on.

What the range of values are for each floating-point type is implementation defined. The convention, however, is to represent float values in accordance with the IEEE-754 32 bit floating point type and to represent double values in accordance with the IEEE-754 64 bit floating point type.

Due to this convention, the range of a float is usually ±3.4 × 10±38 (with ~7 significant digits) and the range of a double is usually ±1.7 × 10±308 (with ~15 significant digits).

Floating-point literals

Floating-point literals can be expressed in base 10 for any of the three floating-point types.

Here are some examples of floating-point literals for the same value:

Values with the optional 'e' symbol are followed by an integer and this indicates to what power of 10 should the floating-point literal value be multiplied by.

The default type of a floating-point literal is double, but you may add a suffix to change that:

Floating-point literal suffix Type
double
f float
l long double
std::numeric_limits

Integer and floating-point types are collectively called arithmetic types and their properties can be queired for your particular C++ implementation using std::numeric_limits

The following link is a program that uses std::numeric_limits to obtain the implementation's minimum and maximum values for short int, int, long int, float, double, and long double.

limits.cpp

Using std::numeric_limits requires that you include the <limits> header file in your program.

You can learn more about std::numeric_limits at http://en.cppreference.com/w/cpp/types/numeric_limits