coderz.py

Keep Coding Keep Cheering!

C++ sizeof() and typedef Operator

Accessing Elements
sizeof():

The sizeof keyword is a compile-time unary operator that determines the size of a variable or data type in bytes. sizeof() operator is used in different way according to the operand type, i.e,

  • When operand is a Data Type.
  • When operand is an expression. 

Syntax:

sizeof(type)
OR
sizeof expression
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:
Size of char : 1
Size of int : 4
Size of short int : 2
Size of long int : 8
Size of float : 4
Size of double : 8
Size of wchar_t : 4
Typedef:

The typedef keyword allows programmers to create new names for types such as int or, more commonly in C++, templated types; it literally means “type definition.” Typedef can be used to improve the clarity of your code as well as to make changes to the underlying data types that you use easier.

Syntax:

typedef existing_name alias_name
Example:
#include <iostream>
 int main(){
	typedef unsigned int ui;
	ui i = 5, j = 8;
	std::cout << "i = " << i << std::endl;
	std::cout << "j = " << j << std::endl;
	return 0;
}
Output:
i = 5
j = 8

typedef and Pointers: Pointers can also be given an alias name using typedef.

For instance,

typedef int* Ptr ;
Ptr x, y, z;

Note: also read about the Operators in C++

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Comment

Your email address will not be published. Required fields are marked *

Advertisement