
- 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
Why is sizeof() implemented as an operator in C++?
sizeof is not a real operator in C++. It is merely special syntax that inserts a continuing equal to the size of the argument. sizeof doesn’t want or have any runtime support. Sizeof cannot be overloaded because built-in operations, such as incrementing a pointer into an array implicitly depends on it.
The C standard specifies that sizeof should be implemented as an operator. In most compilers, the value of sizeof is substituted by a constant equal to it at the compile time itself.
example
#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 sizeof operator in C++?
- Sizeof operator in C
- What is the use of sizeof Operator in C#?
- Result of sizeof operator using C++
- Implement your own sizeof operator using C++
- Why is not sizeof for a struct equal to the sum of sizeof of each member in C/C++?
- Why isn't sizeof for a struct equal to the sum of sizeof of each member in C/C++?
- How is encapsulation implemented in C#?
- Why can C++ templates only be implemented in the header file?
- What is the purpose of ‘as’ operator in C#?
- sizeof() function in PHP
- Anything written in sizeof() is never executed in C
- How optimized Bitmap Filtering is implemented?
- What is an exclamation “!” operator in JavaScript?
- What is an assignment operator in C#?

Advertisements