Categories: C++

C++ sizeof() and typedef Operator

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

Share
Published by
Rabecca Fatima

Recent Posts

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

1 month ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

2 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

2 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

3 months ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

3 months ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

3 months ago