C++ program to find the size of all data types using the sizeof operator
ADVERTISEMENTS
In this article, you will learn how to determine the size of all data types of the c++ programming language.
You should have knowledge of the following topics in c++ programming to understand this program:
- C++ Data Types
- C++
main
function - C++
sizeof
operator - C++
cout
object
Source Code
// C++ program to find the size of all data types using the sizeof operator
#include <bits/stdc++.h>
using namespace std;
// Driver Program
int main() {
// Calculate the size of short int data type
cout <<"The size of short int: " << sizeof(short int) << " bytes\n";
// Calculate the size of unsigned short int data type
cout <<"The size of unsigned short int: " << sizeof(unsigned short int) << " bytes\n";
// Calculate the size of unsigned int data type
cout <<"The size of unsigned int: " << sizeof(unsigned int) << " bytes\n";
// Calculate the size of int data type
cout <<"The size of int: " << sizeof(int) << " bytes\n";
// Calculate the size of long int data type
cout <<"The size of long int: " << sizeof(long int) << " bytes\n";
// Calculate the size of unsigned long int data type
cout <<"The size of unsigned long int: " << sizeof(unsigned long int) << " bytes\n";
// Calculate the size of long long int data type
cout <<"The size of long long int: " << sizeof(long long int) << " bytes\n";
// Calculate the size of unsigned long long int data type
cout <<"The size of unsigned long long int: " << sizeof(unsigned long long int) << " bytes\n";
// Calculate the size of signed char data type
cout <<"The size of signed char: " << sizeof(signed char) << " byte\n";
// Calculate the size of unsigned char data type
cout <<"The size of unsigned char: " << sizeof(unsigned char) << " byte\n";
// Calculate the size of char data type
cout <<"The size of char: " << sizeof(char) << " byte\n";
// Calculate the size of float data type
cout <<"The size of float: " << sizeof(float) << " bytes\n";
// Calculate the size of double data type
cout <<"The size of double: " << sizeof(double) << " bytes\n";
// Calculate the size of long double data type
cout <<"The size of long double: " << sizeof(long double) << " bytes\n";
return 0;
}
Output
The size of short int: 2 bytes
The size of unsigned short int: 2 bytes
The size of unsigned int: 4 bytes
The size of int: 4 bytes
The size of long int: 8 bytes
The size of unsigned long int: 8 bytes
The size of long long int: 8 bytes
The size of unsigned long long int: 8 bytes
The size of signed char: 1 byte
The size of unsigned char: 1 byte
The size of char: 1 byte
The size of float: 4 bytes
The size of double: 8 bytes
The size of long double: 16 bytes
Explanation
In this given program, we used the sizeof
operator to determine the size of available data types in the c++ programming language.