C++ sizeof Operator (original) (raw)

Last Updated : 16 Jan, 2026

The sizeof operator is a **unary, compile-time operator in C++ used to determine the **memory size (in bytes) of variables, data types, constants, as well as user-defined types such as structures, unions, and classes.

For example:

C++ `

#include using namespace std;

int main() { cout << sizeof(int); return 0; }

`

**Explanation: The above code outputs the size of the integer data type for the compiler on which the program is executed. Here, it is 4 bytes.

This article covers the syntax, usage, and common examples of sizeof operator in C++:

Syntax of sizeof in C++

**sizeof (expression)

Examples of sizeof in C++

The below examples demonstrate the common usage of sizeof in C++:

1. Size of Different Primitive Data Types

C++ `

#include <bits/stdc++.h> using namespace std;

int main() {

cout << "Size of char: " << sizeof(char) << endl;
cout << "Size of int: " << sizeof(int) << endl;
cout << "Size of float: " << sizeof(float) << endl;
cout << "Size of double: " << sizeof(double) << endl;
cout << "Size of long: " << sizeof(long);
return 0;

}

`

Output

Size of char: 1 Size of int: 4 Size of float: 4 Size of double: 8 Size of long: 8

2. Size of Different Variables

C++ `

#include <bits/stdc++.h> using namespace std;

int main() { int a; float b; char g;

// Printing size of all three variables
cout << "Size of a:  " << sizeof(a) << endl;
cout << "Size of b:  " << sizeof(b) << endl;
cout << "Size of g:  " << sizeof(g);

return 0;

}

`

Output

Size of a: 4 Size of b: 4 Size of g: 1

3. Size of an Array Using sizeof

C++ `

#include <bits/stdc++.h> using namespace std;

int main() { int arr[] = {1, 2, 3, 5, 6};

  // Finding the length of array
  int n = sizeof(arr) / sizeof(arr[0]);
  cout << n;
  return 0;

}

`

4. Size of Class

C++ `

#include <bits/stdc++.h> using namespace std;

class A { int x; A(int val = 10) : x(val){} };

int main() { // Finding size of class A (size of the objects // of class A) cout << "Size of Class a: " << sizeof(A); return 0; }

`