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 TYPE | MEMORY (BYTES) | RANGE | FORMAT SPECIFIER |
---|---|---|---|
short int | 2 | -32,768 to 32,767 | %hd |
unsigned short int | 2 | 0 to 65,535 | %hu |
unsigned int | 4 | 0 to 4,294,967,295 | %u |
int | 4 | -2,147,483,648 to 2,147,483,647 | %d |
long int | 4 | -2,147,483,648 to 2,147,483,647 | %ld |
unsigned long int | 4 | 0 to 4,294,967,295 | %lu |
long long int | 8 | -(2^63) to (2^63)-1 | %lld |
unsigned long long int | 8 | 0 to 18,446,744,073,709,551,615 | %llu |
signed char | 1 | -128 to 127 | %c |
unsigned char | 1 | 0 to 255 | %c |
float | 4 | %f | |
double | 8 | %lf | |
long double | 12 | %Lf |
Post a Comment