HomeC++C++ Program to Find the Size of Primitive Data Types

C++ Program to Find the Size of Primitive Data Types

In this post, you’ll learn how to write a Program to Find the Size of Primitive Data Types in C++.

This lesson will help you learn how to find the Size of Primitive Data Types with a certain function, using the C++ language. Let’s look at the below source code.

How to Find the Size of Primitive Data Types in C++?

RUN CODE SNIPPET

Source Code

#include <iostream>
using namespace std;

int main() 
{    
    cout << "Size of char: " << sizeof(char) << " byte" << endl;
    cout << "Size of int: " << sizeof(int) << " bytes" << endl;
    cout << "Size of float: " << sizeof(float) << " bytes" << endl;
    cout << "Size of double: " << sizeof(double) << " bytes";

    return 0;
}

Output

Size of char: 1 byte
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes

Let’s break down the code and elaborate the steps, to understand better

#include <iostream>

  • This line which is called the header file is used in every C++ codding. #include statement tells the compiler to use available files and <iostream>is the name of the specific that we have used in this code.
  • The <iostream> file stands for Input and Output statements.

using namespace std;

  • The C++ has a standard library that has files for different functions and this line is used to access the standard file for input and output statements.

int main();

  • This line usually controls the function of the code, as it calls the functions to perform their tasks.
  • The int main()shows that the input value is a type of integer, once the program is executed the function returns to the main function, by using the statement ‘return 0;’.

{ }

  • The opening ‘ { ‘ and the closing ‘ } ‘ curly braces mark the start and the finish of the main function
  • Every statement and value between these braces belong to the main function.

The above statements are the main factors that support the function of the source code. Now we can look into the working and layout of the code’s function.

  1. In this code the main function statement is sizeof(DataType); , using this function we can find the size of the Data Type
  2. Display the output statements with the cout and using the Insertion Operator ‘ << ‘, we can insert the function to find the Size of the Data Type.
  3. When using sentences, write them within quotes ” “, " Size of char: " followed by ‘ << ‘ and the function.
  4. When we run the program, the function is executed and the answer is displayed in it’s place.
  5. End the function with return 0; this returns the function to main( ).

Now that you have understood how the source code performs its function, try it yourself with following different functions to it understand better.

  • sizeof (char)
  • sizeof (int)
  • sizeof (short int)
  • sizeof (long int)
  • sizeof (signed long int)
  • sizeof (unsigned long int)
  • sizeof (float)
  • sizeof (double)
  • sizeof (wchar_t)

Note: The ‘ << endl ‘ in the code is used to end the current line and move to the next line, to understand how it works exclude it from the code, move it around and work with it.

Share:

Leave A Reply

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

You May Also Like

In this post, you will learn how to Generate Multiplication Table using C++ programming language. This lesson will teach you...
  • C++
  • January 30, 2022
In this post, you will learn how to Display Fibonacci Sequence using C++ programming language. This lesson will teach you...
  • C++
  • January 30, 2022
In this post, you will learn how to Find GCD of two Numbers using C++ programming language. This lesson will...
  • C++
  • January 30, 2022