Basic Types

  • Other than char  (which has a size of exactly one byte), none of the fundamental types has a standard size specified (but a minimum size, at most). This does not mean that these types are of an undetermined size, but that there is no standard size across all compilers and machines; each compiler implementation may specify the sizes for these types that fit the best the architecture where the program is going to run.

    • "This rather generic size specification for types gives the C++ language a lot of flexibility to be adapted to work optimally in all kinds of platforms, both present and future."

Chars

  • char

    • Exactly one byte in size. At least 8 bits.

  • char16_t

    • Not smaller than char. At least 16 bits.

  • char32_t

    • Not smaller than char16_t. At least 32 bits.

  • wchar_t

    • Can represent the largest supported character set.

Integer

Signed
  • signed char

    • Same size as char. At least 8 bits.

  • short  / signed short int  / signed short  / short int

    • Not smaller than char. At least 16 bits.

  • int  / signed int

    • Not smaller than short. At least 16 bits.

  • long  / signed long int  / signed long  / long int

    • Not smaller than int. At least 32 bits.

  • long long  / signed long long int  / signed long long  / long long int

    • Not smaller than long. At least 64 bits.

Unsigned
  • unsigned char

    • same size as their signed counterparts.

  • unsigned short  / unsigned short int

    • same size as their signed counterparts.

  • unsigned  / unsigned int

    • same size as their signed counterparts.

  • unsigned long  / unsigned long int

    • same size as their signed counterparts.

  • unsigned long long  / unsigned long long int

    • same size as their signed counterparts.

Floats

  • float

  • double

    • Precision not less than float

  • long double

    • Precision not less than double

Boolean

  • bool

    • Typically 1 byte (implementation-defined)

Void

  • void

    • No storage, as it represents the absence of a value.

    • Cannot create variables of type void .

  • Used for:

    • Functions that return nothing

    • Generic pointers ( void* )

    • Indicating no parameters (C-style)

Null Pointer (C++11)

  • decltype(nullptr)  / std::nullptr_t

    • The type of the literal nullptr .

    • Size:

      • 32-bit system -> 4 bytes

      • 64-bit system -> 8 bytes

  • Represents a null pointer constant .

  • Can convert to any pointer type, but it is not a pointer type itself.

auto p = nullptr;              // type is std::nullptr_t
std::nullptr_t np = nullptr;   // explicit
  • Why it exists:

    • Replaces NULL  and 0  for pointer initialization

    • Prevents overload ambiguity

    void f(int);
    void f(char*);
    
    f(nullptr);  // calls f(char*)
    
    • Using 0  or NULL  could be ambiguous.

  • nullptr  vs void* :

    • void*  -> โ€œa pointer to something (unknown type)โ€

    • nullptr  -> โ€œpoints to nothingโ€

    • They are not the same thing.