C++ is a very convenient and easy language to learn as it’s a language that provides access to even minor and critical details. Wrapped up within these extraordinary features, we have a unique operator known as the “sizeof” function. The sizeof c++ is one of the fundamental operators used for reference while programming.
The Sizeof Operator
The sizeof is an operator where the meaning is apparent from the name “sizeof” as it is used to evaluate the size of the:
The sizeof operator is a compile-time operator, which derives these data size(s) in bytes.
The syntax of sizeof is straightforward. It is as simple as:
sizeof(data_type_as_value);
Code The Fun Up
Code the fun up is an exclusive learning exercise by GeekonPeak, which will enhance your coding skill (especially the specific topic you’re looking into). From this fantastic exercise, you’ll learn to apply the concept we’ve learned flawlessly and test the code live, delivering confidence and helping you grasp the core concept via observation. The Level of Difficulty of the code can vary from Level 1 to Level 5.
Rules are Simple:
- Type the code Manually, Don’t cheat!
- Try to understand each line and part of the code.
- If you’re facing some errors in the code, Recheck!
- If you can’t find the error, copy and paste our code to Online GDB (https://www.onlinegdb.com/). If our code’s running fine, Check your Code and compiler.
- If something’s wrong with the code, it’s not working or throwing errors; you may let us know via Contact Us Page.
- If you’ve any doubts, you can ask in the comments, but don’t try to cheat if you want to learn.
Difficulty: Level 1
The Code Begins
#include <iostream>
using namespace std;
int main(){
//Size of Primitive Data types
cout<<"char: "<< sizeof(char)<<" bytes"<<endl;
cout<<"int: "<< sizeof(int)<<" bytes"<<endl;
cout<<"float: "<< sizeof(float)<<" bytes"<<endl;
cout<<"double: "<< sizeof(double )<<" bytes"<<endl;
//Size of Data Types Modifiers
cout<<"unsigned int: "<< sizeof(unsigned int)<<" bytes"<<endl;
cout<<"short: "<< sizeof(short)<<" bytes"<<endl;
cout<<"long: "<< sizeof(long)<<" bytes"<<endl;
cout<<"long long: "<< sizeof(long long)<<" bytes"<<endl;
cout<<"long double: "<< sizeof(long double)<<" bytes"<<endl;
//sizeof using variable names
//Example 1 using integer
cout<<"sizeof using variable names"<<endl;
int a = 21;
cout<<"a is "<< sizeof(a)<<" bytes"<<endl;
//Example 2 using double
double temp {22.24};
cout<<"temp is "<< sizeof(temp)<<" bytes"<<endl;
return 0;
}
CTFU: Explanation
In this sizeof program, we firstly included the primary header of C++, i.e., iostream, and used the fundamental namespace standard. After that, inside the main function, our actual code begins. Firstly we’ll begin by outputting the sizes of primitive data types, i.e., char, int, double, and double. Then we’ll print off the sizes of data types modifiers, i.e., unsigned int, short, long, long long, long double. After printing all the primitive data types and data type modifiers, we’ll move on to printing the sizes of variables that we’ll create.
So as you can see in Example 1, we’ve created an integer variable named “a” with a value of 21, and in the following line, we’ve printed its size for evaluation. Following Example 2, we’ve created another variable named “temp” with the value of 22.24 and, in the following line, we’ve again printed the size of “temp” to evaluate the size. If you saw it carefully, we’ve also added an end-line character (endl) at the end of every line, which allows you to separate the line reducing the clutter and the confusion in the output. And as the size is in bytes, we’ve added it to help you make more sense of the output.
Conclusion
So that’s all about the sizeof operator in the C++ hope you’ve understood it well and are following the latest C++ blogs. Besides hope, you loved our new format of CTFU (code the fun up), and you enjoyed coding while learning as it focuses on more practical knowledge than theoretical knowledge.
Similarly, you must also know that GeekonPeak has just begun, and it is looking for some loyal supporters so we, the new-gen geeky friends, can grow and create more extensive and easy to grasp courses for you (for free). You can follow us on Facebook, Instagram & Twitter as well. Also, don’t miss our newsletter subscription. We provide exclusive blogs, tips, tricks, and advice to program efficiently and market smartly. We also promote some of our partners with excellent experiences to share with you. Until next time 🙂