A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/cpp/typeid-operator-in-c-with-examples/ below:

typeid operator in C++ with Examples

typeid operator in C++ with Examples

Last Updated : 12 Jul, 2025

typeid is an operator in C++. 


Syntax:  

typeid(type);
OR
typeid(expression);


Parameters: typeid operator accepts a parameter, based on the syntax used in the program:  


Return value: This operator provides the runtime type information of the specified parameter and hence that type information is returned, as a reference to an object of class type_info.
Usage: typeid() operator is used in different way according to the operand type. 

  1. When operand is a variable or an object. CPP
    // C++ program to show the use of typeid operator
    
    #include <iostream>
    #include <typeinfo>
    using namespace std;
    
    int main()
    {
        int i, j;
        char c;
    
        // Get the type info using typeid operator
        const type_info& ti1 = typeid(i);
        const type_info& ti2 = typeid(j);
        const type_info& ti3 = typeid(c);
    
        // Check if both types are same
        if (ti1 == ti2)
            cout << "i and j are of"
                 << " similar type" << endl;
        else
            cout << "i and j are of"
                 << " different type" << endl;
    
        // Check if both types are same
        if (ti2 == ti3)
            cout << "j and c are of"
                 << " similar type" << endl;
        else
            cout << "j and c are of"
                 << " different type" << endl;
    
        return 0;
    }
    

    Output
    i and j are of similar type
    j and c are of different type
  2. When operand is an expression. CPP
    // C++ program to show the use of typeid operator
    
    #include <iostream>
    #include <typeinfo>
    using namespace std;
    
    int main()
    {
        int i = 5;
        float j = 1.0;
        char c = 'a';
    
        // Get the type info using typeid operator
        const type_info& ti1 = typeid(i * j);
        const type_info& ti2 = typeid(i * c);
        const type_info& ti3 = typeid(c);
    
        // Print the types
        cout << "ti1 is of type "
             << ti1.name() << endl;
    
        cout << "ti2 is of type "
             << ti2.name() << endl;
    
        cout << "ti3 is of type "
             << ti3.name() << endl;
    
        return 0;
    }
    

    Output: 
    ti1 is of type f
    ti2 is of type i
    ti3 is of type c

     


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4