
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
What is sizeof operator in C++?
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. The 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. When the sizeof operator is applied to an object of type char, it yields 1. When the sizeof operator is applied to an array, it yields the total number of bytes in that array, not the size of the pointer represented by the array identifier.
Example
Let's look at an example that lists the size of some inbuilt types −
#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; }
Output
This will give the output −
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
- Related Articles
- What is the use of sizeof Operator in C#?
- Sizeof operator in C
- Why is sizeof() implemented as an operator in C++?
- Result of sizeof operator using C++
- Implement your own sizeof operator using C++
- sizeof() function in PHP
- What is a Ternary operator/conditional operator in C#?
- What is @ operator in Python?
- Anything written in sizeof() is never executed in C
- How to use sizeof() operator to find the size of a data type or a variable in C#
- What is "is not" operator in Python?
- What is operator binding in Python?
- What is the operator in MySQL?
- What is dot operator in Java?
- What is increment (++) operator in JavaScript?

Advertisements