DEV Community

Igor
Igor

Posted on

C++ Primitive Data Types

  • Fundamental data types implemented directly by the C++ language
  • Character types
  • Integer types
    • signed and unsigned
  • Floating-point types
  • Boolean type
  • Size and precision is often compiler-dependent
    • #include <climits>

Type Sizes

  • Expressed in bits
  • The more bits the more values that can be represented
  • The more bits the more storage required
Size (in bits) Representable Values
8 256 2^8
16 65,536 2^16
32 4,294,967,296 2^32
64 18,446,744,073,709,551,615 2^64

Character Types

  • Used to represent single characters, ‘A’, ‘X’, ‘@’
  • Wider types are used to represent wide character sets
Type Name Size / Precision
char Exactly one byte. At least 8 bits.
char16_t At least 16 bits.
char32_t At least 32 bits.
wchar_t Can represent the largest available character set

Integer Types

Type Name Size / Precision
signed short int At least 16 bits.
signed int At least 16 bits.
signed long int At least 32 bits.
signed long long int At least 64 bits.
Type Name Size / Precision
unsigned short int At least 16 bits.
unsigned int At least 16 bits.
unsigned long int At least 32 bits.
unsigned long long int At least 64 bits.
  • In addition to those shown in the table, it’s possible to store both signed and unsigned integers in the character data type
  • If you want to declare a signed integer you don’t need to use the signed keyword, since by default integers are signed.
  • Similarly, if you want an integer type that stores very large numbers, you can simply declare the type as long long. And you’ll get a signed long long integer.
  • If you want unsigned integers, that is integers that are 0 or positive values only, then you’re required to use the unsigned keyword.
  • By default, all integers are signed.

Floating-point Type

  • Used to represent non-integer numbers
  • Represented by mantissa and exponent (scientific notation)
  • Precision is the number of digits in the mantissa
  • Precision and size are compiler dependent
Type Name Size / Typical Precision Typical Range
float / 7 decimal digits 1.2 x 10^-38 to 3.4 x 10^38
double No less than float / 15 decimal digits 2.2 x 10^-308 to 1.8 x 10^308
long double No less than double/ 19 decimal digits 3.3 x 10^-4932 to 1.2 x 10^4932

Boolean Type

  • Used to represent true and false
  • Zero is false
  • Non-zero is true
Type Name Size / Precision
bool Usually 8 bits true or false

Top comments (0)