Sizeof() Operator:
This Operator accepts one parameter, that can be either a variable type or a variable itself and returns the size in bytes of that type or object: Sometimes it is helpful to know the size, in bytes of a type of data. Since the size of C++'s built-in types can differ between computing environments, knowing the size of a variable in advance, in all situations, is not possible. To solve the problem, C++ includes the sizeof compile-time operator, which has these General form:
sizeof( data-type ); //Size of Data type:
sizeof variable-name; //Size of Any Variable:
The first Statement returns the size of the specified data type, and the second returns the size of the specified variable. As we can see, if we want to know the size of a data type, such as int or char, we must enclose the type name in parentheses. if we want to know the size of a variable, no parentheses are Needed, although we can use them.
To see how Sizeof Operator works, try the following short Program.
#include<iostream>
using namespace std;
main()
{
cout<<"Integer data type" <<endl;
cout<<"size of int is " <<sizeof(int)<<endl;
cout<<"size of signed int is " <<sizeof(signed int)<<endl;
cout<<"size of unsigned int is " <<sizeof(unsigned int)<<endl;
cout<<"size of short is " <<sizeof(short)<<endl;
cout<<"size of signed short is " <<sizeof(signed short)<<endl;
cout<<"size of unsigned short is " <<sizeof(unsigned short)<<endl;
cout<<"size of long is " <<sizeof(long)<<endl;
cout<<"size of signed long is " <<sizeof(signed long)<<endl;
cout<<"size of unsigned long is " <<sizeof(unsigned long)<<endl;
cout<<"\n\n character data type" <<endl;
cout<<"size of char is " <<sizeof(char)<<endl;
cout<<"size of signed char is " <<sizeof(signed char)<<endl;
cout<<"size of unsigned char is " <<sizeof(unsigned char)<<endl;
cout<<"size of wchar_t is " <<sizeof(wchar_t)<<endl;
cout<<"\n\n Floating point data type:"<<endl;
cout<<"size of float is " <<sizeof(float)<<endl;
cout<<"size of Double is " <<sizeof(double)<<endl;
cout<<"size of long double is " <<sizeof(long double)<<endl;
return 0;
}
sizeof( ) Operator
Integer data type
size of int is 4
size of signed int is 4
size of unsigned int is 4
size of short is 2
size of signed short is 2
size of unsigned short is 2
size of long is 4
size of signed long is 4
size of unsigned long is 4
character data type
size of char is 1
size of signed char is 1
size of unsigned char is 1
size of wchar_t is 2
Floating point data type:
size of float is 4
size of Double is 8
size of long double is 16
We can Apply sizeof Operator to any data type. For example, when it is applied to an Array, it return the Number of bytes used by the Array. as Mentioned earlier, sizeof Operator is a compile-time operator. All information Necessary for computing the size of a variable or data type is known during compilation. the size of operator primarily helps we to generate portable code that depends upon the size of the C++ data types. Remember, since the size of types in C++ are defined by the implementation, it is bad style to make assumptions about their sizes in code that we write.