#include <iostream>
using namespace std;
int main() {
cout << "Size of char: " << sizeof(char) << " bytes" << endl;
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 long: " << sizeof(long) << " bytes" << endl;
cout << "Size of long long: " << sizeof(long long) << " bytes" << endl;
return 0;
}Output
Size of char: 1 bytes Size of int: 4 bytes Size of float: 4 bytes Size of double: 8 bytes Size of long: 8 bytes Size of long long: 8 bytes
Size of Data Types in C++
This program helps you understand how much memory different data types use in C++. Every data type occupies a specific number of bytes in RAM. Knowing this helps you write efficient programs, especially when dealing with large amounts of data.
1. Header File
#include <iostream>
This header allows the program to use cout for printing output on the screen.
2. What is sizeof()?
The star of this program is the sizeof operator.
sizeof(data_type)
It tells you how many bytes of memory a data type or variable uses.
A ## byte is the basic unit of memory.
1 byte = 8 bits.
When you write sizeof(int), it returns the memory occupied by the int type on the computer where the program runs.
3. Printing Sizes of Basic Data Types
The program prints the sizes of common data types:
- char
- int
- float
- double
- long
- long long
Example:
cout << "Size of int: " << sizeof(int) << " bytes" << endl;
This prints something like: Size of int: 4 bytes
Each line uses cout to print the size returned by sizeof.
4. Why Sizes Can Be Different on Different Computers
Sizes of data types are ## not always the same on every machine.
This depends on:
- The system architecture (32-bit or 64-bit)
- The compiler (like GCC or Clang)
For example:
- On many 64-bit systems:
- int = 4 bytes
- float = 4 bytes
- double = 8 bytes
- long = 8 bytes
- long long = 8 bytes
- On some 32-bit systems:
- long may be 4 bytes instead of 8
So the sizeof operator helps you know the exact sizes on your system.
5. Why Understanding Data Type Sizes Is Important
Knowing data type sizes helps you:
- Manage memory better
- Write efficient programs
- Understand how variables are stored
- Work with file handling, pointers, and arrays
- Avoid overflow errors (when numbers exceed storage limits)
For example:
- char is only 1 byte — good for storing single characters.
- int is 4 bytes — good for whole numbers.
- double is 8 bytes — good for decimal numbers with more precision.
6. Program Flow (Simple Explanation)
- The program starts running in main().
- It uses the sizeof operator to check the size of each data type.
- It prints the size using cout.
- return 0 ends the program successfully.
Summary
- sizeof tells you how many bytes a data type occupies.
- Data type sizes may differ between systems.
- This program prints the sizes of char, int, float, double, long, and long long.
- Understanding sizes is important for memory management and writing efficient C++ programs.
This is an important foundation lesson for beginners before learning arrays, pointers, and memory allocation.