sizeof() AND typedef() operator in C++

Well, as we know there are different types of the operator in C++. But, before moving into the topic you must know about some terminology like keywords which we discussed earlier, if you are new please do refer to them for better understanding.

In this article, we’ll particularly be discussing two operators which are the sizeof() operator and typedef() operator in brief.

sizeof() operator:

sizeof() is a keyword in C++, it’s actually a compile-time operator to be more specific. Its job is to determine the amount of space taken by a variable, class, datatypes, . . etc in the memory. 

How do we actually use sizeof() in our program?

Answer: It’s pretty simple we write sizeof with parenthesis and within the parenthesis, we write the desired variable or the thing we want to determine the space of, something like this sizeof(int).

So, let’s check the space taken by basic data types through this simple program given below:

#include<iostream>
using namespace std;
int main(){
	cout<<"Size of int: "<<sizeof(int)<<endl;
	cout<<"Size of float: "<<sizeof(float)<<endl;
	cout<<"Size of Short int: "<<sizeof(short int)<<endl;
	cout<<"Size of char: "<<sizeof(char)<<endl;
	cout<<"Size of double: "<<sizeof(double)<<endl;
	cout<<"Size of Boolean: "<<sizeof(bool)<<endl;
	return 0;
}

FbaURukXfPkF2X5MGdcqS6RwyhEYIDerQpls9oJrlc7hfaWh3o6

Credit Goes To Shiva Dash

In the above example, we checked the sizes of different basic data types. Let’s take another example of a variable where we’ll find out the space of a variable.

#include<iostream>
using namespace std;
int main(){
	bool shiva= true;
	cout<<"Size of variable shiva: "<<sizeof(shiva)<<endl;
	return 0;
}

kNuft97tCgs6ZeYkDcHQF3Ka3PLD1YjNIQepIzlYJeXJaoP3hlUDbREwsE9ZML7g1eOgUpbsKl0QMkSmkgJpOAzq1yAgB9MDbbIROWMktW0kftMSebqycpwiBQSCzACSMrhiBw

Credit Goes To Shiva Dash

Let’s take one more example of a sizeof structure:

#include<iostream>
using namespace std;
struct employee{
	int salary;
	char name;
};
int main(){
	int splitsecond=30;
	cout<<"Size of splitsecond: "<<sizeof(splitsecond)<<endl;
	cout<<"Size of structure: "<<sizeof(employee)<<endl;
	return 0;
}

Credit Goes To Shiva Dash

In the above example we found the space taken by a structure it’s showing 8 because the processor reserves some extra space for faster processing although our calculated space would be five as int takes 4 bytes and character takes 1 byte. Let’s move o typedef.

Typedef operator:

It stands for type definition obviously. It is used to remember the data type. Suppose, we don’t want to type the same data type again and again as the world is full of lazy people, am not saying it’s you but whenever you feel so typedef operator is here to help you out.

So, it remembers the data type and when we want to use it instead of specifying the data type again we can use a typedef. So, to use it we write a typedef required datatype new data type. Then, we can use the same datatype using the variable we gave to it.

So, below is an example of how exactly we can use typedef:

#include<iostream>
using namespace std;
int main(){
	typedef unsigned long long big_datatype;
	big_datatype key= 17353647576274;
	big_datatype lock= 213821739172;
	cout<<key<<endl;
	cout<<lock<<endl;
	return 0;
}

RcEayKFxCyfRODFUaDCku7szxlYM7Fgzv1f50wKk3zXXY4ii3fFrfzYmgcrO56iFbUOcY 5A3IfYdJy4prW2U8pH5eRl3wECC2bXAEgnTz ekAbooJAXNKWE4ZHcX5YTSh8 qw

Credit Goes To Shiva Dash

So, what’s happening in the above example is it would have been hard if we used unsigned long data type again and again for key and lock variables. So, instead of typing the whole data type again, we used typedef to specify the datatype with a new data type named big datatype which made our work a lot easier. So, enjoy learning, enjoy coding thank you.

Please write comments or WhatsApp if you find anything incorrect, or you want to share more information about the topic discussed above.

Default image
Shiva Patra
Hello, this is G. Shiva. Patra, your content writer for this website, is currently pursuing BCA for a graduation degree. I am here to write content based on C++ and similar kind of stuff. I like the tech world as it grows really fast and you have great things to learn every day which keep you away from boredom. So, I have some knowledge on very different topics related to tech like the dark web, deep web, onion rooting, IoT, Cybersecurity, and I always keep track of new devices and software used by them as every tech enthusiast does.
Articles: 9

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply