Latest web development tutorials

C ++ sizeof operator

C ++ operator C ++ operator

sizeof is a keyword, it is a compile-time operator, used to determine the variable or data type in bytes.

sizeof operator can be used to obtain a class, structure, union, and other user-defined data type size.

Sizeof use the following syntax:

sizeof (data type)

Wherein, data type to calculate the size of data types, including classes, structures, unions, and other user-defined data types.

Try the following examples to understand C ++, sizeof usage. Copy and paste the following C ++ program to 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 << "Size of wchar_t : " << sizeof(wchar_t) << endl;
   return 0;
}

When the above code is compiled and executed, it produces the following results, the results will differ according to the use of the 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 : 4

C ++ operator C ++ operator