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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago