Size of int, float, double, char

Beginner-friendly C++ program that shows how much memory different data types use, using the sizeof operator.

BeginnerTopic: Basic Programs
Back

C++ Size of int, float, double, char Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#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

Understanding 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:

char
int
float
double
long
long long

Example:

This prints something like:

Size of int: 4 bytes

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:

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)

1.The program starts running in main().
2.It uses the sizeof operator to check the size of each data type.
3.It prints the size using cout.
4.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.

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.

Table of Contents