C++ Program to Find the Size of int, float, double and char

In this post, we will learn how to find the size of int, float, double and char using the C++ Programming language.

In the below program, we will declare four variables of data type char, int, float, and double. Then, size of each variable is evaluated using sizeof operator.

So, without further ado, let’s begin this tutorial.

C++ Program to Find the Size of int, float, double and char

// C++ Program to Find the Size of int, float, double and char
#include <iostream>
using namespace std;

int main(){
    
    cout << "Size of a character (char): " << sizeof(char) << " bytes" << endl;
    cout << "Size of an Integer (int):  " << sizeof(int) << " bytes" << endl;
    cout << "Size of a Floating point (float): " << sizeof(float) << " bytes" << endl;
    cout << "Size of Double (double): " << sizeof(double) << " bytes" << endl;
    
    return 0;
}

Output

Size of a character (char): 1 bytes
Size of an Integer (int):  4 bytes
Size of a Floating point (float): 4 bytes
Size of Double (double): 8 bytes

How Does This Program Work?

    cout << "Size of a character (char): " << sizeof(char) << " bytes" << endl;
    cout << "Size of an Integer (int):  " << sizeof(int) << " bytes" << endl;
    cout << "Size of a Floating point (float): " << sizeof(float) << " bytes" << endl;
    cout << "Size of Double (double): " << sizeof(double) << " bytes" << end

The sizeof() is an operator that evaluates the size of data types, constants and variables. It is a compiler time operator as it returns the size of any variable at the compilation time.

Conclusion

I hope after going through this post, you understand how to find the size of int, float, double and char using the C++ Programming language.

If you have any doubt regarding the program, then contact us in the comment section. We will be delighted to assist you.

Also Read:

Leave a Comment

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