1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C++ Inbuilt Data Types

Discussion in 'C++' started by BiplabKamal, May 14, 2016.

  1. In previous chapter we came to know about data types. There are data types defined by the language called in-built data types. C++ also allows to create new data types. In this chapter we will concentrate only on in-built data types. We will discuss user defined data types later.

    C++ basically has seven fundamental data types. Following table shows those types and key words used:


    New types are created with type modifiers using keywords: signed, unsigned, short and long from those basic types. void is not a regular data type but used only in few cases like for return type and pointer. Following table shows possible types, their sizes in memory and minimum and maximum value it can store:

    Built-in data types in C++:


    In the above table you will notice that some data types are same in size and interpretation. For example int and long both are of 4 bytes. So what is the use of two data types?

    Actually they may be different depending on the machine hardware architecture where long will be bigger than int. But they will follow the following rules. Signed and unsigned modifier does not change the size but the left most bit is reserved for the sign(positive or negative) for signed type.

    1 == sizeof(char) <= sizeof(short) <= sizeof (int) <= sizeof(long) <= sizeof(long long)
    sizeof(double) <= sizeof(long double)

    You should test the data type size in the system you are working. The sample code given below will help you to test all types which will show the size, interpretation and the actual content of the memory allocated for the variable.

    Other than the basic types mentioned there are some derived data types: Array and Pointer.


    Array is a sequence of data stored in the memory. It is declared and defined with the following syntax

    <element type> <array variable name>[<size>]{<initialization list>};

    For example an array variable named IntArray of integer which can store 5 integer elements is defined below. Elements are initialized with 1,2,3,4 and 5.Initialization is not mandatory.

    int IntArray[5]{1,2,3,4,5};

    Each element is accessed using a zero based index like IntArray[0], IntArray[1], IntArray[2], IntArray[3] and IntArray[4]. You can modify a particular element or get the value of an element like:

    IntArray[4] = 10; // Modify the last element
    int i = IntArray[4]; // Read the last element


    Pointer is a special type of variable which stores the starting address of a variable. The size of pointer depends on the size of the address space the application can access. In a 32-bit application the size is 4 bytes and for 64-bit application the size is 8 bytes. You can create a pointer variable for a particular type. You can also create a void pointer which can store the address of any type but to access the value of the variable you have to convert that pointer to that type. Void pointer works as a placeholder. Pointer variable can be initialized with a special value nullptr which is equivalent to 0 address. You cannot assign a constant literal to a pointer. You have to assign the address of a variable. Following is the syntax:

    <data type>* <variable name>{&<variable name> or nullptr};

    For example a pointer to integer variable can be declared and assigned as

    int n{ 0 }; // variable
    int* pInt{nullptr}; //Pointer with no address
    pInt = &n; //Assign the address of the variable n
    *pInt = 100; // Modify the variable n pointed by the pointer

    As shown in the above example address of a variable is specified by & followed by the variable name. While accessing the variable via the pointer variable * is used followed by the pointer variable name.

    In the following program we will use sizeof operator to get the size of a data type. We are using cout and bitset objects to display memory content to the console. We could have used C standard library function printf() we already discussed but using cout is much easier. We will discuss cout and cin when discussing standard input output feature of C++ standard library. We also have used template class which is part of advanced C++. All those unknown feature we used just to display the memory content of a variable. For the time being the focus is on the size and the content of a data storage in the memory. The program will display the size, the meaning full value and the binary data in the memory.

    #include <iostream>
    using namespace std;
    template<class T>
    void ShowMemory(const T& var) // Generic function to display the binary value of a variable
        cout << "binary value =" << bitset<sizeof(T) * 8>(var) << endl;
    // Following function will display variable size, value and memory content for different inbuilt type
    void ShowSize()
        int size{ 0 };
        // Boolean type
        size = sizeof(bool);
        cout << "Size of bool =" << size << endl;
        bool b = true;
        cout << "bool value=" << b << endl;
        cout << endl;
        // Unsigned character type
        unsigned char uc = 'c';
        cout << "Size of unsigned char =" << sizeof(unsigned char) << endl;
        cout << "unsigned char value=" << uc << endl;
        cout << endl;
        // Signed character type
        signed char sc = 'c';
        cout << "Size of signed char =" << sizeof(signed char) << endl;
        cout << "Signed char value =" << sc << endl;
        cout << endl;
        // Wide character type
        wchar_t wc = 'c';
        cout << "Size of wchar_t =" << sizeof(wchar_t) << endl;
        cout << "wide char value =" << wc << endl;
        cout << endl;
        // Unsigned short integer type
        unsigned short usi = 100;
        cout << "Size of unsigned short =" << sizeof(unsigned short) << endl;
        cout << "unsigned short value=" << usi << endl;
        cout << endl;
        // Signed short integer type
        size = sizeof(signed short);
        cout << "Size of signed short =" << size << endl;
        signed short s = -100;
        cout << "signed short value=" << s << endl;
        cout << endl;
        // Signed integer type
        size = sizeof(signed int);
        cout << "Size of signed int =" << size << endl;
        signed int si = -100;
        cout << "signed int value=" << si << endl;
        cout << endl;
        // Unsigned integer type
        size = sizeof(unsigned int);
        cout << "Size of unsigned int =" << size << endl;
        unsigned int ui = 100;
        cout << "usigned int value=" << ui << endl;
        cout << endl;
        // Signed long integer type
        long int sl = -100;
        cout << "Size of signed long int =" << sizeof(signed long int) << endl;
        cout << "signed long int value =" << sl << endl;
        cout << endl;
        // Unsigned long integer type
        unsigned long int usl = 100;
        cout << "Size of unsigned long int =" << sizeof(unsigned long int) << endl;
        cout << "unsigned long int value =" << usl << endl;
        cout << endl;
        // Single floating point type
        float f = -100.77;
        cout << "Size of float =" << sizeof(float) << endl;
        cout << "float value =" << f << endl;
        cout << endl;
        // Signed long long integer type    
        signed long long sll = -100;
        cout << "Size of signed long long =" << sizeof(long long) << endl;
        cout << "long long int value=" << sll << endl;
        cout << endl;
        // Unsigned long long integer type
        unsigned long long usll = 100;
        cout << "Size of unsigned long long =" << sizeof(unsigned long long) << endl;
        cout << "unsigned long long int value=" << usll << endl;
        cout << endl;
        // Double floating point
        size = sizeof(double);
        cout << "Size of double =" << size << endl;
        double d = -100.77;
        cout << "double value=" << d << endl;
        cout << endl;
        // long double floating point 
        size = sizeof(long double);
        cout << "Size of long double =" << size << endl;
        long double ld = -100.77;
        cout << "long double value=" << ld << endl;
        cout << endl;
        // Array of integer
        int IntArray[5]{1,2,3,4,5};
        size = sizeof(IntArray);
        cout << "Size of 5 element array =" << size << endl;
        cout << "Array values = " << IntArray[0]<<" " << IntArray[1] << " " << IntArray[2] << " " << IntArray[3] << " " << IntArray[4] << " " << endl;
        cout << endl;
        // Pointer to integer
        int n{ 0 };
        int* pInt{nullptr};
        pInt = &n;
        *pInt = 100;
        size = sizeof(pInt);
        cout << "Size of pointer =" << size << endl;
        cout << "value = " << *pInt << endl;
        cout << endl;
    int main()
        mreturn 0;
    Create a text file named inbuiltdatatypes.cpp and copy the above code in the file, save and compile the code. I am using windows 7 64-bit OS. I have installed LLVM and visual studio community 2015 because LLVM does not install the C++ library which includes headers and libraries. I have used following command to compile and create the executable. You might use the visual studio in Windows but I recommend to use command line to better understand the compilation process.

    clang++ inbuiltdatatypes.cpp -fms-compatibility-version=19.00 -o inbuilt.exe -std=c++14

    Current directory should be same as the directory where inbuiltdatatypes.cpp is saved. You can use any other name for the .cpp file from your choice. Above command will create the inbuilt.exe. Run the exe file with command inbuilt and it will generate following output:

    Size of bool =1
    bool value=1
    binary value =00000001
    Size of unsigned char =1
    unsigned char value=c
    binary value =01100011
    Size of signed char =1
    Signed char value =c
    binary value =01100011
    Size of wchar_t =2
    wide char value =99
    binary value =0000000001100011
    Size of unsigned short =2
    unsigned short value=100
    binary value =0000000001100100
    Size of signed short =2
    signed short value=-100
    binary value =1111111110011100
    Size of signed int =4
    signed int value=-100
    binary value =11111111111111111111111110011100
    Size of unsigned int =4
    usigned int value=100
    binary value =00000000000000000000000001100100
    Size of signed long int =4
    signed long int value =-100
    binary value =11111111111111111111111110011100
    Size of unsigned long int =4
    unsigned long int value =100
    binary value =00000000000000000000000001100100
    Size of float =4
    float value =-100.77
    binary value =11111111111111111111111110011100
    Size of signed long long =8
    long long int value=-100
    binary value =1111111111111111111111111111111111111111111111111111111110011100
    Size of unsigned long long =8
    unsigned long long int value=100
    binary value =0000000000000000000000000000000000000000000000000000000001100100
    Size of double =8
    double value=-100.77
    binary value =1111111111111111111111111111111111111111111111111111111110011100
    Size of long double =8
    long double value=-100.77
    binary value =1111111111111111111111111111111111111111111111111111111110011100
    Size of 5 element array =20
    Array values = 1 2 3 4 5
    binary value =00000000000000000000000000000001
    binary value =00000000000000000000000000000010
    binary value =00000000000000000000000000000011
    binary value =00000000000000000000000000000100
    binary value =00000000000000000000000000000101
    Size of pointer =8
    value = 100
    binary value =00000000000000000000000001100100
    So far we have been declaring a variable specifying it’s type explicitly. Good news is that the latest C++ (since C++11) allows you to declare a variable without specifying the data type provided you initialize the variable with an expression whose type can be evaluated at compile time. In this case compiler decides the data type based on the initialization expression. A keyword ‘auto’ is used in place of type identifier. This is called automatic type deduction. See the following code decelerations:

    long l;
    auto var{ l }; // var becomes long
    auto var{ 100 }; // var becomes int
    auto var{ 10.5 }; // var becomes double
    auto var{ 'c' }; // var becomes char
    auto var{ "adsd" }; // var becomes const char*
    auto var{ L"dsdsf"}; // var becomes const wchar_t*

    ‘auto’ keyword also can be used in place of function return type provided the function has single return statement. The function return type is deduced from the return statement. auto helps when using user defined types which are long and complex. It reduce the typing of long and complex name.

Share This Page