Basic data types

From Tux
Revision as of 22:43, 20 January 2018 by Williams (talk | contribs)
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 s lost so the actual number represented would only be 22222222222222200000......0.

char

bool

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 wchar_t