Size of int, float, double, char
Program to display the size of different data types
BeginnerTopic: Basic Programs
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;
}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
The sizeof operator returns the size in bytes of a data type or variable. This is useful for understanding memory requirements. Note that sizes may vary depending on the system architecture (32-bit vs 64-bit).
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.