auto 변수와 자료형 타입

2020. 11. 9. 15:35


auto를 활용하면 변수형을 별도로 선언하지 않아도 컴퓨터가 알아서 변수형을 파악해준다.

 

#include <iostream>
#include <typeinfo>
using namespace std;

int main() {
	ios::sync_with_stdio(NULL);
	cin.tie(NULL);
	
	auto s1 = "치킨먹자";
	cout << s1 << " : " << typeid(s1).name() << "\n";

	auto i1 = 100;
	cout << i1 << " : " << typeid(i1).name() << "\n";

	auto d_or_f1 = 0.353;
	cout << d_or_f1 << " : " << typeid(d_or_f1).name() << "\n";

	auto arr = { 1,2,3 };
	cout << arr.size() << " : " << typeid(arr).name() << "\n";

	return 0;
}