Is it possible to print a variable"s type in standard C++?

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for Is it possible to print a variable"s type in standard C++?

Can You Print a Variable's Type in C++? 🤔

Have you ever wondered if it's possible to print the type of a variable in C++? Maybe you find yourself in a situation where you need to debug your code or simply understand the type of a variable during runtime. Well, the good news is that there is a simple solution to this problem!

The Challenge: Printing a Variable's Type 📝

Let's take a look at an example to better understand the challenge at hand:

int a = 12;
cout << typeof(a) << endl;

In this code snippet, we have a variable a of type int. The intention is to print the type of the variable a using cout.

The expected output of this code would be:

int

However, if you run this code as is, you'll quickly discover that there is a problem. You might encounter an error similar to:

error: use of undeclared identifier 'typeof'

Now, let's explore the solution to this problem!

Solution: Using typeid and name() 🚀

To print the type of a variable in C++, we can make use of the typeid operator in combination with the name() function.

#include <iostream>
#include <typeinfo>

int main() {
    int a = 12;
    const std::type_info& type = typeid(a);
    std::cout << type.name() << std::endl;

    return 0;
}

Running the above code will yield the expected result:

i

In this solution, we include the <typeinfo> header to gain access to the necessary functionality. Then, we define a variable type of type const std::type_info& and assign it the result of typeid(a). This retrieves the type information of variable a.

Finally, by calling type.name(), we obtain a const char* containing the name of the type. In this case, i represents the int type.

Take It Further: Printing Other Types 🎉

The use of typeid and name() is not limited to only primitive types. You can also use it with other types such as classes, structures, or even user-defined types!

Let's see an example with a class:

#include <iostream>
#include <typeinfo>

class MyClass {
    // class implementation goes here
};

int main() {
    MyClass obj;
    const std::type_info& type = typeid(obj);
    std::cout << type.name() << std::endl;

    return 0;
}

When executing this code snippet, you will get an output similar to:

7MyClass

Here, 7MyClass indicates that the type of obj is the class MyClass. The 7 corresponds to the number of characters in the class name.

Share Your Discoveries! 🌟

Knowing how to print the type of a variable can be incredibly useful for debugging or understanding code during runtime. Now that you have this knowledge, go ahead and share it with your fellow developers! They might find it handy as well.

If you have any questions, suggestions, or want to learn more about C++, leave a comment below. Let's start a conversation and improve our coding skills together!

Keep coding! 🚀

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my