Updated April 12, 2023
Introduction to C++ sizeof()
The sizeof() is an operator in C and C++. It is an unary operator which assists a programmer in finding the size of the operand which is being used. The result of this operator is an integral type which is usually signified by size_t. This operator is usually used with data types which can be primitive data types like integer, float, pointer, etc. It can also give size of complex datatypes like structure, union, etc. It is a compile time operator which will tell the size of any data type and compute the size of operand.
Syntax:
Below is the syntax of using sizeof():
sizeof(type)
sizeof expression
The sizeof function in the first type, will give the output as an size in bytes of the object of the type which is sent. The second type is the size in bytes of the object which is in the type of expression. The size will be the size once the expression is evaluated. In both these versions the constant expression of the standard type that is size_t.
How sizeof() Operator work in C++?
The sizeof() operator can be used to find the size of datatype or expressions. They work in a very easy manner of taking these as arguments and work on them by returning the size in bytes.
Code:
#include <iostream>
using namespace std;
int main() {
cout << "Size of char : " << sizeof(char) << endl;
cout << "Size of int : " << sizeof(int) << endl;
cout << "Size of expression 5 + 8 is : " << sizeof(5 + 8) << endl;
return 0;
}
The above code helps us in getting the size of different data types. We have first used the standard library <iostream>. It helps us in using all inbuilt functions. The sizeof function is a part of these inbuilt function which are present in iostream library. Then by making use of cout we are printing the output of the sizeof() function. When this function is called, we find that there are four data types which are being used. These are char, int, float and double.
As stated earlier the sizeof function will help us in getting the size of every datatype. Hence it uses these data types as an argument and return the data size of each data type. We have also taken size of expression where we are adding 2 integers. It will calculate these and the result will also be an integer. Hence the output for this will be also 4. The output of above function and code will be the number of bytes each variable uses. To check a few the output for char will be as below.
Output:
Examples of C++ sizeof()
Given below are the examples mentioned:
Example #1
Operand as a data type.
Code:
#include <iostream>
using namespace std;
int main()
{
cout << "The size of char data type is " << sizeof(char)<<"\n";
cout << "The size of int data type is " << sizeof(int)<<"\n";
cout << "The size of float data type is "<< sizeof(float)<<"\n";
cout << "The size of double data type is " << sizeof(double)<<"\n";
return 0;
}
The above code will give the data size in bytes of each datatype which is sent as an argument. All the sizes will be in bytes.
Output:
Example #2
Operand as an expression.
Code:
#include <iostream>
using namespace std;
int main()
{
int a = 7;
float d = 15.21;
cout << "The addition of int and float is a float as follows: " << a + d;
cout << "\nThe size of the expression is " << sizeof(a + d);
return 0;
}
The above code with return the size of the data type of the resulting expression. Here we have declared two variables. One variable is an integer and the second variable is a float. We are now adding these two variables in the expression of which we will be finding the size of. The result of this expression will be a float. Hence the sizeof will be the size of float data type.
Output:
You can see the result of addition is a float. Also the size of the expression is that of the data type float, that is 4.
Example #3
Finding number of elements in the array.
Code:
#include <iostream>
using namespace std;
int main()
{
int array1[] = { 1,5,76,89,23,06 };
cout << "The number of elements which are present in the array are : "
<<(sizeof(array1) / sizeof(array1[0]));
return 0;
}
In addition to giving the size of expressions and data types, the sizeof operator can also be used to find the number of elements in an array. Here we have defined an array ‘array1’. We have a few elements added to it. In order to get the count of these elements and get exactly how many elements are present in it we can simply make use of the sizeof operator.
We specify the array name and the first index that is array1[0] which helps us in starting he count from beginning. It will start from index 0 and count till the end. Once it reached the end it will display the number of elements. It will be returned by this sizeof() function that we have.
Output:
Conclusion
The sizeof() operator is a function which returns the size of any data type, expression, array, etc. It takes the data type or expression as a part of argument which is mandatory and returns the result which is size of that data type in bytes. If it is an array it will return the number of elements present in it. It is a very useful function when we have to allocate memory. We can calculate the size and allocate memory as per requirement and as a result save a lot of space which otherwise will be occupied.
Recommended Articles
This is a guide to C++ sizeof(). Here we discuss how sizeof() operator work in C++ along with examples respectively. You may also have a look at the following articles to learn more –