Difficulty: Beginner
Programming Language: C++

In the previous tutorial we briefly covered how to create a variable, assign a value to the variable we've created and output the variables value to the terminal window.

In the following sections we'll be expanding on what we learned to cover the different variable types that are available in C++ and the operations that can be performed on each type.

There are three basic types in C++:

  • Boolean
    A boolean can only represent a value of true or false and are useful for storing "on / off" states within an application.

  • Integral
    These types represent whole number values. These are useful for counting and for storing more complex program states than a boolean value can represent.

  • Floating Point
    These types also represent numbers but unlike integral types, the floating point types support decimal precision. Because of a limited number of bits used to represent floating point numbers which attempts to create the maximum range possible, a compromise with the precision of floating point must be made. Floating point numbers are able to represent very small numbers close to zero with a very high precision, but as the value moves further away from zero the precision drops. This is generally acceptable as it aligns with the idea of significant figures used in maths and science.

The boolean type is the only type which belongs to the boolean family.

Below are two tables to expand on the integral and floating point types. The sizes and ranges listed below are the typical default values for PC application compilers, however these sizes and value ranges are not guaranteed. To be certain of what sizes your variables are check the documentation for your compiler.

Integral Types Available In C++
Variable Type Description Unsigned Range Signed Range
char A very small integer, also used to represent a single text character. 0 to 28-1 -27 to 27-1
short A small integer. 0 to 216-1 -215 to 215-1
int The standard integer type of C++. 0 to 232-1 -231 to 231-1
long A large integer. In most application compilers this is the same size as int. 0 to 232-1 -231 to 231-1
long long A very large integer. 0 to 264-1
-263 to 263-1

 

Floating Point Types Available In C++
Variable Type Description Value Range Special Values
float

A 32 bit wide or "single precision" floating point variable type. Is able to represent numerical values containing decimal points.

1 bit is used to indicate if the value is negative, 23 bits store the significant figures of the value, and the remaining 8 bits store the exponent. The 23 bits used to store the significant figures gives us a range of 223 values, then the 8 bit exponent allows us to move the decimal place up to 127 orders of magnitude either side of 0.

±1.18×10−38 (smallest exponent)

±3.4×1038 (largest exponent)

  • Not A Number (NaN)
  • Positive Infinity
  • Negative Infinity
double

A 64 bit wide or "double precision" floating point variable type. Compared to a float this type can store number much larger or at much higher resolution.

1 bit is used to indicate if the value is negative, 52 bits store the significant figures of the value, and the remaining 11 bits store the exponent. The 52 bits used to store the significant figures gives us a range of 252 values, then the 11 bit exponent allows us to move the decimal place up to 1023 orders of magnitude either side of 0.

±2.23×10−308 (smallest exponent)

±1.80×10308 (largest exponent)

  • Not A Number (NaN)
  • Positive Infinity
  • Negative Infinity