In this Program, you’ll learn how to find size of operator.

Here declares 4 variables of type int, float, double and char. Then, the size of each variable is evaluated using size of operator.
To nicely understand this example to find size of operator, you should have the knowledge of following C++ programming topics:

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);
Source Code:
#include <iostream>
using namespace std;
int main() 
{    
    cout << "Size of char: " << sizeof(char) << " byte" << 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;
    return 0;
}




DATA TYPEMEMORY (BYTES)RANGEFORMAT SPECIFIER
short int2-32,768 to 32,767%hd
unsigned short int20 to 65,535%hu
unsigned int40 to 4,294,967,295%u
int4-2,147,483,648 to 2,147,483,647%d
long int4-2,147,483,648 to 2,147,483,647%ld
unsigned long int40 to 4,294,967,295%lu
long long int8-(2^63) to (2^63)-1%lld
unsigned long long int80 to 18,446,744,073,709,551,615%llu
signed char1-128 to 127%c
unsigned char10 to 255%c
float4%f
double8%lf
long double12%Lf

Post a Comment

 
Top