C++ sizeof Operator Last Updated : 09 Dec, 2024 Comments Improve Suggest changes Like Article Like Report The sizeof operator is a unary compile-time operator used to determine the size of variables, data types, and constants in bytes at compile time. It can also determine the size of classes, structures, and unions.Let's take a look at an example: C++ #include <iostream> using namespace std; int main() { cout << sizeof(int); return 0; } Output4Explanation: 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++:Table of ContentSyntax of sizeof in C++Examples of sizeof in C++Find Size of Different Primitive Data TypesFind the Size of Different VariablesFind the Size of an Array Using sizeofFind the Size of ClassSyntax of sizeof in C++The sizeof operator can be used to find the size of a type or a variable in C++ as shown:sizeof (type)sizeof (expression)Examples of sizeof in C++The below examples demonstrate the common usage of sizeof in C++:Find Size of Different Primitive Data Types C++ #include <bits/stdc++.h> using namespace std; int main() { // Finding the size of different data types 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; } OutputSize of char: 1 Size of int: 4 Size of float: 4 Size of double: 8 Size of long: 8Find the 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; } OutputSize of a: 4 Size of b: 4 Size of g: 1Find the 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; } Output5Find the 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; } OutputSize of Class a: 4 Comment More infoAdvertise with us R raj2002 Follow Improve Article Tags : Technical Scripter C++ Technical Scripter 2022 cpp-operator Practice Tags : CPPcpp-operator Explore C++ Programming Language 5 min read C++ OverviewIntroduction to C++ Programming Language 3 min read Features of C++ 5 min read History of C++ 7 min read Interesting Facts about C++ 2 min read Setting up C++ Development Environment 8 min read Difference between C and C++ 3 min read C++ BasicsUnderstanding First C++ Program 4 min read C++ Basic Syntax 4 min read C++ Comments 3 min read Tokens in C 4 min read C++ Keywords 2 min read Difference between Keyword and Identifier in C 3 min read C++ Variables and ConstantsC++ Variables 4 min read Constants in C 4 min read Scope of Variables in C++ 7 min read Storage Classes in C++ with Examples 6 min read Static Keyword in C++ 5 min read C++ Data Types and LiteralsC++ Data Types 7 min read Literals in C 4 min read Derived Data Types in C++ 4 min read User Defined Data Types in C++ 4 min read Data Type Ranges and Their Macros in C++ 3 min read C++ Type Modifiers 4 min read Type Conversion in C++ 4 min read Casting Operators in C++ 5 min read C++ OperatorsOperators in C++ 9 min read C++ Arithmetic Operators 4 min read Unary Operators in C 5 min read Bitwise Operators in C 6 min read Assignment Operators in C 4 min read C++ sizeof Operator 3 min read Scope Resolution Operator in C++ 4 min read C++ Input/OutputBasic Input / Output in C++ 5 min read cin in C++ 4 min read cout in C++ 2 min read Standard Error Stream Object - cerr in C++ 2 min read Manipulators in C++ 4 min read C++ Control StatementsDecision Making in C (if , if..else, Nested if, if-else-if ) 7 min read C++ if Statement 3 min read C++ if else Statement 3 min read C++ if else if Ladder 3 min read Switch Statement in C++ 5 min read Jump statements in C++ 4 min read C++ Loops 7 min read for Loop in C++ 6 min read Range-Based for Loop in C++ 3 min read C++ While Loop 3 min read C++ do while Loop 4 min read C++ FunctionsFunctions in C++ 8 min read return Statement in C++ 4 min read Parameter Passing Techniques in C 3 min read Difference Between Call by Value and Call by Reference in C 4 min read Default Arguments in C++ 5 min read Inline Functions in C++ 6 min read Lambda Expression in C++ 4 min read C++ Pointers and ReferencesPointers and References in C++ 5 min read C++ Pointers 8 min read Dangling, Void , Null and Wild Pointers in C 6 min read Applications of Pointers in C 4 min read Understanding nullptr in C++ 3 min read References in C++ 5 min read Can References Refer to Invalid Location in C++? 2 min read Pointers vs References in C++ 5 min read Passing By Pointer vs Passing By Reference in C++ 5 min read When do we pass arguments by pointer? 5 min read Like