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

 

In C++, the sizeof operator is used to find how much memory a data type uses in bytes. This is useful for understanding memory usage and writing efficient code.

Below is a simple C++ program that prints the sizes of int, float, double, and char.


Example Code

#include <iostream>
using namespace std;

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

    return 0;
}

Sample Output

Size of int: 4 bytes  
Size of float: 4 bytes  
Size of double: 8 bytes  
Size of char: 1 bytes
  • sizeof(type) is used to find the memory size of a given data type.
  • Common data types like int, float, double, and char are fundamental in C++.
  • Knowing their sizes is important for memory optimization, especially in embedded and system-level programming.
  • This program is helpful for beginners to understand how data types are stored in memory and why size differences matter in real-world applications.

Conclusion

The sizeof operator helps determine the storage size of different data types in C++. It’s a basic but important concept for beginners learning memory and data representation.

Tip: Always use sizeof instead of assuming sizes, especially when writing portable code across different systems.

 

Leave a Comment

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

Scroll to Top