Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is the sizeof Operator in C++?
2.1.
Syntax
2.2.
C++
3.
How sizeof() Operator works?
3.1.
C++
4.
Examples
4.1.
Case1: When an Operand is of Data type
4.2.
C++
4.3.
Case 2: When an Operand is of Class Type
4.4.
C++
4.5.
Case 3: When an Operand is of Array Type
4.6.
C++
4.7.
Case 4: When an Operand is an Expression
4.8.
C++
4.9.
Case 5: When an Operand is of Pointer Type
4.10.
C++
5.
Advantages of Using sizeof Operator
6.
Frequently Asked Questions
6.1.
What type does sizeof() return in C++?
6.2.
What does the sizeof() operator do?
6.3.
What is the difference between size() and sizeof() in C++?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

sizeof Operator in CPP

Author yuvatimankar
0 upvote

Introduction

C++ is a free, general purpose. Case-sensitive programming language that supports object-oriented, generic, and procedural programming. Operators are symbols that run operations on values and variables. They form the foundation of any programming language.

sizeof operator in cpp

In this article, we will discuss the sizeof the operator in C++. So let’s get started!

What is the sizeof Operator in C++?

The sizeof Operator in C++ is a unary operator that calculates the size of data type, constants, and variables at compile time. The size of char, which on most systems is 1 byte (8 bits), is a multiple of the size returned by the operator.size_t is the type of value returned from the sizeof() operator. It is an integer type defined in the header file<stddef.h>.

The operand can be of the following in the sizeof operator in C++:

  • Type name: For using the C++ sizeof operator with a type name, it must be enclosed in parentheses
     
  • An expression: The sizeof operator in C++ can be used with/ without parentheses when used with an expression; also, the expression is not evaluated.

Syntax

The syntax of the sizeof operator, when the operand is the data type, is as follows:

  • C++

C++

sizeof(data_type);
You can also try this code with Online C++ Compiler
Run Code

Here, data_type can be the data type of data, structure, variable, union, constants, or any other data type defined by the user.

How sizeof() Operator works?

The sizeof operator in C++ can be used to return the data type/ expression size and work directly by taking these values as an operand. And this operator works on them by returning their size in bytes.

  • C++

C++

#include<iostream>

using namespace std;

int main() {  

   cout << "Size of int = " << sizeof(int) << endl;

   cout << "Size of float = " << sizeof(float) << endl;

   cout << "Size of char = " << sizeof(char) << endl;

   return 0;

}
You can also try this code with Online C++ Compiler
Run Code

Output:

output

Examples

Let’s see the cases where the size of the operator can be used with examples.

Case1: When an Operand is of Data type

  • C++

C++

#include<iostream>

using namespace std;

int main() {

   cout << "Size of int = " << sizeof(int) << endl;

   cout << "Size of char = " << sizeof(char) << endl;

   cout << "Size of float = " << sizeof(float) << endl; 

   cout << "Size of double = " << sizeof(double) << endl;

   return 0;

}
You can also try this code with Online C++ Compiler
Run Code

Output:

output

Here, we use the C++ sizeof operator to evaluate the sizes of four different data types. The output clarifies that the int data type takes up 4 bytes of computer memory, float takes up 4, double takes up 8, and char takes up just 1 byte.

Case 2: When an Operand is of Class Type

  • C++

C++

#include<iostream>

using namespace std;

class TClass {

   // Data attribute of the class.

   int x;

};

int main() {

   TClass obj;

   cout << "Size of class is: " << sizeof(obj) << endl;
   return 0;

}
You can also try this code with Online C++ Compiler
Run Code

Output:

output

The result for the above-mentioned program shows that the class object uses 4 bytes of memory. Because the object only has one data attribute—a four-byte integer—it was expected that this would happen. The size of the object will increase to 8 bytes if we add one more integer to the class.

Case 3: When an Operand is of Array Type

  • C++

C++

#include<iostream>

using namespace std;

int main() {  

   int arr[] = {10, 20, 30, 40, 50};  

   cout << "Size of array is: " << sizeof(arr) << endl;

   return 0;

}
You can also try this code with Online C++ Compiler
Run Code

Output:

output

We declare an array with five integer values in the program above. We used the sizeof operation in C++ to assess the array. The sizeof the (array) value should be five times the size of the int, or 5 * 4 = 20 bytes, according to the calculation. Using the sizeof operator, the output gives the same result.

Case 4: When an Operand is an Expression

  • C++

C++

#include<iostream>

using namespace std;

int main() {

   int a;

   double b;

   cout << "Size of expression is: " << sizeof(a + b) << endl;

   return 0;

}
You can also try this code with Online C++ Compiler
Run Code

Output:

output

We declared two variables in the above example, a of type integer and b of type double. The sum of an integer and a double will be saved as a double, which requires 8 bytes of computer memory. Hence the result of sending the expression to the sizeof operator is 8 bytes.

Case 5: When an Operand is of Pointer Type

  • C++

C++

#include<iostream>

using namespace std;

int main() {

   int * pntr1 = new int(7);

   cout << "Size of pntr1 is: " << sizeof(pntr1) << endl;

   cout << "Size of *pntr1 is: " << sizeof( * pntr1) << endl;

  

double * pntr2 = new double(32.1);

   cout << "Size of pntr2 = " << sizeof(pntr2) << endl;

   cout << "Size of *pntr2 = " << sizeof( * pntr1) << endl;

   return 0;

}
You can also try this code with Online C++ Compiler
Run Code

Output:

output

From the output given, we can observe that the size of both the pointers is the same, measuring 8 bytes. However, the size of the *pointer varies due to the discrepancy in the size of the data type pointed to by the pointer in C++. The first pointer indicates a 4-byte integer; the second pointer indicates an 8-byte.

Advantages of Using sizeof Operator

Advantages of using the sizeof() operator are as follows:

  • It helps with the system type on which the program is running
     
  • It helps to calculate and find the size of an array or the number of elements present in an array
     
  • It helps check the memory taken by user-defined data types

Also see, Abstract Data Types in C++

Frequently Asked Questions

What type does sizeof() return in C++?

The sizeof() returns an unsigned integer value which stands for the amount of memory in bytes occupied by the operand. 

What does the sizeof() operator do?

The sizeof the operator in C++ gives the amount of storage in bytes needed to store an object of the operand type. This operator enables us to avoid specifying machine-dependent data sizes in our programs.

What is the difference between size() and sizeof() in C++?

The size () operator returns the number of characters in the string, whereas sizeof() is used to get the actual size of any type of data in bytes.

Conclusion

In this article, we discussed the sizeof operators in C++. We started with the introduction to C++ and operators in C++, then we saw the sizeof operator and finally saw examples of it. We hope this blog has helped you enhance your knowledge of the sizeof Operator in C++. If you want to learn more, then check out our articles:

Refer to our guided paths on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But suppose you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must have a look at the problemsinterview experiences, and interview bundles for placement preparations.

Nevertheless, consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

 

Live masterclass