C++ Beginners – Fundamental Types

C++ Logo - C++ Beginners Guide
C++ Logo – C++ Beginners Guide

C++ Beginners – Fundamental Types covers the fundamental types in C++. Anyone coming from higher-level languages such as C#, #Java or many others will note that “string” is not a fundamental type in C++. The following are fundamental types.

  • signed short
  • unsigned short
  • signed int
  • unsigned int
  • signed long
  • unsigned long
  • signed long long
  • unsigned long long
  • float
  • double
  • long double
  • char
  • bool

As mentioned in C++ Beginners Guide Introduction, C++ is a compiled language. Consequently, there are differences in these types’ sizes with different operating systems and different compilers on the same operating system. This can be demonstrated with a simple program run on differing systems. The program specifies all the fundamental types and then uses the “sizeof” operator to output the variable’s size in bytes.

#include <iostream>
using namespace std;

int main() {
    signed short typeSignedShort = 1;
    unsigned short typeUnsignedShort = 1;

    signed int typeSignedInt = 1;
    unsigned int typeUnsignedInt = 1;

    signed long typeSignedLong = 1;
    unsigned long typeUnsignedLong = 1;

    signed long long typeSignedLongLong = 1;
    unsigned long long typeUnsignedLongLong = 1;

    float typeFloat = 1;
    double typeDouble = 1;
    long double typeLongDouble = 1l;
    char typeChar = '1';


    cout << "Size of a signed short is:" << sizeof(typeSignedShort) << endl;
    cout << "Size of a unsigned short is:" << sizeof(typeUnsignedShort) << endl;

    cout << "Size of a signed int is:" << sizeof(typeSignedInt) << endl;
    cout << "Size of a unsigned int is:" << sizeof(typeUnsignedInt) << endl;

    cout << "Size of a signed long is:" << sizeof(typeSignedLong) << endl;
    cout << "Size of a unsigned long is:" << sizeof(typeUnsignedLong) << endl;

    cout << "Size of a signed long long is:" << sizeof(typeSignedLongLong) << endl;
    cout << "Size of a unsigned long long is:" << sizeof(typeUnsignedLongLong) << endl;

    cout << "Size of a float is:" << sizeof(typeFloat) << endl;
    cout << "Size of a double is:" << sizeof(typeDouble) << endl;
    cout << "Size of a long double is:" << sizeof(typeLongDouble) << endl;
    cout << "Size of a char is:" << sizeof(typeChar) << endl;
    return 0;
}

Output Size of each fundamental type with different operating systems and compilers

TypeWindows MicrosoftWindows MinGwWindows CygwinLinux GCC
signed short2222
unsigned short2222
signed int4444
unsigned int4444
signed long4448
unsigned long4448
signed long long8888
unsigned long long8888
float4444
double8888
long double8121216
char1111
bool1111
Table showing the size of the fundamental types in C++ with differing operating systems and compilers.

These differences mean that even when the source code is compiled on a specific operating system, it will not always work on more than one. When iterating over an array of long doubles, the stored variable’s memory address will be different. This does not mean it will be an issue; however, it is worth knowing that it can be a problem.

Related Articles – C++ Beginners – Fundamental Types

Related Post

2 thoughts on “C++ Beginners – Fundamental Types

Leave a Reply

Your email address will not be published. Required fields are marked *