Basic data types

From Tux
Jump to: navigation, search

C++ contains many data types, the basic data types (also fundamental or primitive) are those which are built into the core language. More complex data types are typically built upon the foundation of these basic data types.

The most common basic data types are int, double, char, and bool.

int

The int data type is used for representing positive and negative integers (whole numbers). Typically an int uses 4 bytes (32 bits) of storage. The first bit of an int is used for the sign (positive or negative) and the remaining 31 bits are used to encode the integer in binary. Thus, the typical range of an int is -231 to 2^31 - 1. Note that the positive range is - 1 because of the representation of zero is included in the positive numbers but there is no "negative zero".

double

The double data type is used for representing real numbers. It typically uses 8 bytes (64 bits) and encodes the value in three parts:

  • 1 bit for a sign (e.g. negative)
  • 11 bits for an exponent (e.g. 125)
  • 52 bits for a mantissa (e.g. 5.883)

The value is then given as sign mantissa x 10exponent. The major advantage of using a double is that it can store a very large range of values (up to 10308), but it only contains 15 decimal place accuracy. For example, a number consisting of the digit 2 repeated many times may be written as 2.2222....2 x 10300, but when encoded everything after the 15th digit is lost so the actual number represented would only be 22222222222222200000......0.

char

The char (pronounced like char as in charbroil) data type is used for representing characters. It typically uses 1 byte (8 bits) and encodes the character according to the ASCII table. Note that ASCII only contains 128 values (range 0 to 127), although 1 byte can encode 256 different values. The values outside of the range 0 to 127 are not standardized and rarely used.

In C++ when explicitly using characters, they must be enclosed in single quotes:

char ch;

ch = 'A'; // The character A (ASCII value 65) is stored in ch
ch = A;   // This is an error, unless A is another variable or constant

cout << 'X' << endl; // Displays the character X
cout << X << endl;   // This is an error, unless X is another variable or constant

The ASCII table contains collated sequences of digits and letters, so 'B' + 3 is equal to 'E' for example.

bool

The bool data type is used for storing true/false Boolean values. It only needs 1 bit (0 or 1) but, as the smallest amount of addressable memory is 1 byte, it takes 1 byte of storage. The value of true is encoded as 1, while false is encoded as 0.

bool z = true;

z = false;

if (z)
     cout << "The value of z was true" << endl;     // This line will not display
else
     cout << "The value of z was false" << endl;    // This line will display

Modifiers

Various modifiers exist to change the usage of bits, thus changing the range. Some details are provided below:

  • short - less storage, less range than basic data type
  • long - more storage, more range than basic data type
  • long long - more storage, more range than long data type
  • signed - uses sign bit for value representation, making the range span positive values only
  • unsigned - uses sign bit for sign representation, making the range span positive and negative values

These modifiers should be placed before the data type (e.g. short int x = 3;).

Other basic data types

In addition to the data types given above, there are data types of float (a smaller and less precise version of double) and three different character types: wchar_t, char16_t, and char32_t which use more than 1 byte of storage for characters.

Links