Uses of Typeid
As per the passed operand type, the typeid operator can be used in various ways as discussed below.
When the operand is an expression
Lets now implement the code for typeid operator in C++, when the operand is an expression.
Algorithm
- In the first step, we have declared three variables, i.e. i of int data type, c of char data type and d of double data type.
- Then we assigned the type_info of all the variables using the C++ typeid() operator.
-
Then we checked the types of all the variables and got the expected result.
#include <iostream>
#include <typeinfo>
using namespace std;
int main()
{
int i = 9;
char c = 'x';
double d = 11.22;
// Get the type info using typeid operator
const type_info& t1 = typeid(i * d);
const type_info& t2 = typeid(i * c);
const type_info& t3 = typeid(c);
// Printing the types of t1, t2 and t3
cout << "t1 is of type "
<< t1.name() << endl;
cout << "t2 is of type "
<< t2.name() << endl;
cout << "t3 is of type "
<< t3.name() << endl;
return 0;
}
Output

You can try by yourself with the help of online c++ compiler.
Explanation
Type of t1 is d which means that t1 is of double data type because here d indicates double.
Type of t2 is i which means that t2 is of int data type because here t2 indicates integer.
Type of t3 is c which means that t3 is of char data type because t3 indicates character.
When the operand is a variable or an object
Lets now implement the code for typeid operator in C++, when the operand is a variable or an object.
Algorithm
- In the first step, we have declared three variables, i.e. a and b of int data type and c of char data type.
- Then we assigned the type_info of all the variables using the C++ typeid() operator.
-
Then we compared the types of all the variables and got the expected result.
#include <iostream>
#include <typeinfo>
using namespace std;
int main()
{
int a, b;
char c;
// Get the type info using typeid operator
const type_info& t1 = typeid(a);
const type_info& t2 = typeid(b);
const type_info& t3 = typeid(c);
// Here we will check if t1 and t2 types are similar
if (t1 == t2)
cout << "a and b are of the same type" << endl;
else
cout << "a and b are of different type" << endl;
// Here we will check if t2 and t3 types are similar
if (t2 == t3)
cout << "a and c are of same type" << endl;
else
cout << "b and c are of different type" << endl;
return 0;
}
Output

Explanation
Type of a and b is of int data type so they are of same type.
Type of b and c is of int and char data type, respectively so they are of different type.
Must Read Lower Bound in C++
Frequently Asked Questions
What is the Typeid operator in C++?
A program can use the typeid operator to get the derived type of an object referred to by a reference or a pointer.
Why Typeid () function is used?
The typeid operator helps us determine an object's type at run time.
What is the difference between running time and time complexity?
Time complexity is the theoretical concept about the asymptotic behavior of an algorithm but running time is the time taken by the program to execute, which depends on the programming language, compiler as well as processor.
Why is C++ known as an Object-Oriented Programming Language?
C++ is known as an Object-Oriented Programming Language as it views a problem or a task in terms of objects involved rather than the procedure to do it.
Why has C++ considered the backbone of OS and Browsers?
Almost every operating system, like Windows, Linux, and Mac OSX, is developed using C++. Many famous Browsers like Mozilla Firefox and Chrome have also been developed using C++.
Conclusion
In this article, we have discussed the details of the typeid operator in C++ with examples.
We hope this blog has helped you enhance your knowledge of the typeid operator in C++.
We encourage you to check out our other articles to learn more about C++.
You can refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. To practice and improve yourself in the interview, you can also check out Top 100 SQL problems, Interview experience, Coding interview questions, and the Ultimate guide path for interviews. Do upvote our blogs to help other ninjas grow. Happy Coding!!