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

Select a Random Element from a Stream

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

1 day ago

Estimate π Using Monte Carlo Method

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

3 weeks ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

3 weeks ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

4 weeks ago

Autocomplete System Implementation

Build an autocomplete system that, given a query string s and a set of possible…

4 weeks ago

Job Scheduler Implementation

Design a job scheduler that accepts a function f and an integer n. The scheduler…

4 weeks ago