
- 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
Implement your own sizeof operator using C++
There is an option that we can implement our own sizeof() operator. The operator sizeof() is a unary operator and is used to calculate the size of any type of data. We can use #define directive to implement our own sizeof() operator which will work exactly same as sizeof() operator.
Here is the syntax to implement own sizeof() operator,
#define Any_name(object) (char *)(&object+1) - (char *)(&object)
Here,
Any_name − The name you want to give to your own sizeof() operator.
Here is an example to implement sizeof() operator in C language,
Example
#include <stdio.h> #define to_find_size(object) (char *)(&object+1) - (char *)(&object) int main() { int x; char a[50]; printf("Integer size : %d\n", to_find_size(x)); printf("Character size : %d\n", to_find_size(a)); return 0; }
Output
Integer size : 4 Character size : 50
In the above program, #define directive is used to declare our own sizeof() operator and it is calculating the size of integer and character type array.
#define to_find_size(object) (char *)(&object+1) - (char *)(&object) …. int x; char a[50]; printf("Integer size : %d\n", to_find_size(x)); printf("Character size : %d\n", to_find_size(a));
- Related Articles
- How will implement Your Own sizeof in C
- Result of sizeof operator using C++
- Implement your own itoa() in C
- Sizeof operator in C
- What is sizeof operator in C++?
- Print with your own font using Python?
- Print with your own font using C#
- Why is sizeof() implemented as an operator in C++?
- What is the use of sizeof Operator in C#?
- Build Your Own Botnet
- How to Build your own website using Django in Python
- Write your own memcpy() in C
- Write your own atoi() in C++
- Can you build your own sundial?
- How to implement MongoDB $or operator

Advertisements