The sizeof is a keyword, but it is a compile-time operator that determines the size, in bytes, of a variable or data type.
The sizeof operator can be used to get the size of classes, structures, unions and any other user defined data type.
Syntax of sizeof OperatorThe syntax of using sizeof is as follows −
sizeof (data type)
Where data type is the desired data type including classes, structures, unions and any other user defined data type.
Example of sizeof OperatorTry the following example to understand all the sizeof operator available in C++. Copy and paste following C++ program in test.cpp file and compile and run this program.
#include <iostream> using namespace std; int main() { cout << "Size of char : " << sizeof(char) << endl; cout << "Size of int : " << sizeof(int) << endl; cout << "Size of short int : " << sizeof(short int) << endl; cout << "Size of long int : " << sizeof(long int) << endl; cout << "Size of float : " << sizeof(float) << endl; cout << "Size of double : " << sizeof(double) << endl; cout << "Size of wchar_t : " << sizeof(wchar_t) << endl; return 0; }
When the above code is compiled and executed, it produces the following result, which can vary from machine to machine −
Size of char : 1 Size of int : 4 Size of short int : 2 Size of long int : 4 Size of float : 4 Size of double : 8 Size of wchar_t : 4More Examples of sizeof in C++
The following examples demonstrate the common usage of the sizeof
operator in C++:
In the following example, we are finding the memory size occupied by different variables.
#include <iostream> using namespace std; int main() { int age = 25; float price = 99.99; char grade = 'A'; // Printing size of variables cout << "Size of age (int): " << sizeof(age) << " bytes" << endl; cout << "Size of price (float): " << sizeof(price) << " bytes" << endl; cout << "Size of grade (char): " << sizeof(grade) << " bytes" << endl; return 0; }
When executed, this program outputs:
Size of age (int): 4 bytes Size of price (float): 4 bytes Size of grade (char): 1 bytesFind the Size of an Array Using sizeof
In the following example, we are finding the total size of an integer array and the number of elements it contains.
#include <iostream> using namespace std; int main() { int scores[] = {85, 90, 78, 92, 88}; // Finding the number of elements in the array int totalSize = sizeof(scores); int elementSize = sizeof(scores[0]); int length = totalSize / elementSize; cout << "Total size of array: " << totalSize << " bytes" << endl; cout << "Size of one element: " << elementSize << " bytes" << endl; cout << "Number of elements: " << length << endl; return 0; }
When executed, this program outputs:
Total size of array: 20 bytes Size of one element: 4 bytes Number of elements: 5Find the Size of a Class
In the following example, we are finding the memory size occupied by an object of a class.
#include <iostream> using namespace std; class Student { int rollNumber; double marks; public: Student(int r = 1, double m = 95.5) : rollNumber(r), marks(m) {} }; int main() { // Finding size of class Student cout << "Size of Student class: " << sizeof(Student) << " bytes" << endl; return 0; }
When executed, this program outputs:
Size of Student class: 16 bytes
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4