How to find out the type of variable C++

To determine the type of variable in C++ there is a function typeid().name() library typeinfo which must be pre-connected with

#include <typeinfo>

typeid().name()

Small demonstration

#include <string> #include <iostream> #include <typeinfo> int main() { // Create four variables of different types int int_var = 1; float float_var = 1.0; char char_var = '0'; std::string str1 = "www.heihei.ru"; // We will display the result of the typeid std::cout << typeid(int_var).name() << std::endl; std::cout << typeid(float_var).name() << std::endl; std::cout << typeid(char_var).name() << std::endl; std::cout << typeid(str1).name() << std::endl; return 0; }

The result

int float char class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > C:\Users\ao\source\repos\typeid\x64\Debug\typeid.exe (process 132920) exited with code 0. Press any key to close this window . . .

All four types are defined.

In other languages

Статьи о C++
Вектор. Часть 0.
Вектор. Часть 1.
Вектор. Часть 2.
Указатели
Классы
SFML
Тетрис на C++ с библиотекой SFML2
SDL
Массив Структур
Решение задач на C++
Как создать пустую строку в C++
Errors C++

Article author: Andrei Olegovich

Banner image