English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
sizeof It is a keyword, which is a compile-time operator used to determine the byte size of a variable or data type.
The sizeof operator can be used to get the size of class, structure, union, and other user-defined data types.
The syntax of sizeof is as follows:
sizeof(data type)
In this example, data type is the data type to be calculated for size, including class, structure, union, and other user-defined data types.
Please try the following example to understand the usage of sizeof in C++. Copy and paste the following C++ program into the test.cpp file, compile and run the 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 << "حجم wchar_t: " << sizeof(wchar_t) << endl; return 0; }
عندما يتم تجميع وكتابة الكود أعلاه وتنفيذه، سيتم إنتاج النتيجة التالية، والتي ستختلف اعتمادًا على الآلة المستخدمة:
حجم char: 1 حجم int: 4 حجم short int: 2 حجم long int: 4 حجم float: 4 حجم double: 8 حجم wchar_t: 4