Size of int, float, double, char
Beginner-friendly C++ program that shows how much memory different data types use, using the sizeof operator.
C++ Size of int, float, double, char Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#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;
}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 bytesUnderstanding Size of int, float, double, char
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
This header allows the program to use cout for printing output on the screen.
---
#include <iostream>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:
Example:
This prints something like:
Each line uses cout to print the size returned by sizeof.
---
cout << "Size of int: " << sizeof(int) << " bytes" << endl;4. Why Sizes Can Be Different on Different Computers
Sizes of data types are
not always the same
on every machine.
This depends on:
For example:
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:
For example:
---
6. Program Flow (Simple Explanation)
---
Summary
This is an important foundation lesson for beginners before learning arrays, pointers, and memory allocation.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Size of int, float, double, char
This C++ program is part of the "Basic Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.