C++ Program to find size of int, float, double and char in Your System

C++ProgrammingServer Side Programming

Data Types in C++

There are many data types in C++ but the most frequently used are int, float, double and char. Some details about these data types are as follows −

  • int - This is used for integer data types which normally require 4 bytes of memory space.

  • float - This is used for storing single precision floating point values or decimal values. float variables normally require 4 bytes of memory space.

  • double - This is used for storing double precision floating point values or decimal values. Double variables normally require 8 bytes of memory space.

  • char - This is used for storing characters. Characters normally require 1 byte of memory space.

sizeof operator in C++

The sizeof operator is used to find the size of the data types. It is a compile time operator that determines the size of different variables and data types in bytes. The syntax of the sizeof operator is as follows −

sizeof (data type);

A program that finds the size of int, float, double and char is as follows −

Example

 Live Demo

#include <iostream>
using namespace std;
int main() {
   cout<<"Size of int is "<<sizeof(int)<<" bytes"<<endl;
   cout<<"Size of float is "<<sizeof(float)<<" bytes"<<endl;
   cout<<"Size of double is "<<sizeof(double)<<" bytes"<<endl;
   cout<<"Size of char is "<<sizeof(char)<<" byte"<<endl;
   return 0;
}

Output

Size of int is 4 bytes
Size of float is 4 bytes
Size of double is 8 bytes
Size of char is 1 byte

In the above program, the sizeof operator is used to find the size of int, float, double and char. This is displayed using the cout object.

cout<<"Size of int is "<<sizeof(int)<<" bytes"<<endl;
cout<<"Size of float is "<<sizeof(float)<<" bytes"<<endl;
cout<<"Size of double is "<<sizeof(double)<<" bytes"<<endl;
cout<<"Size of char is "<<sizeof(char)<<" byte"<<endl;
raja
Published on 26-Sep-2018 12:59:41
Advertisements