영넌 개발로그

[C++ 기초] 데이터 타입 datatype _ 정수형 _ boolean 본문

코딩/C++

[C++ 기초] 데이터 타입 datatype _ 정수형 _ boolean

영넌 2020. 10. 6. 03:12

정수형

  • short         2byte
  • int            4byte
  • long          4byte
  • long long   8byte (64bit)

* 부호 있냐 없냐에 따라 앞에 unsigned가 붙음 :: 크기는 같다! 

 !!! 표현하는 범위만 달라지는 것 !!!

 

 

프로그램을 통해 사이즈가 맞는지 확인해보자

 

#include <iostream>
int main()
{
	using namespace std;
	

	cout << "short size: " << sizeof(short) << endl;
	cout << "int size: " << sizeof(int) << endl;
	cout << "long size: " << sizeof(long) << endl;
	cout << "long long size: " << sizeof(long long) << endl;
	cout << endl;
	short s_var;
	int i_var;
	long l_var;
	long long ll_var;
	//var name 이용
	cout << "short size: " << sizeof s_var << endl;
	cout << "int size: " << sizeof i_var << endl;
	cout << "long size: " << sizeof l_var << endl;
	cout << "long long size: " << sizeof ll_var << endl;
	return 0;
}

 

 

* 각 데이터 타입의 최대 최소

헤더파일 climits 이용

 

 

#include <iostream>
#include <climits>
int main()
{
	using namespace std;
	
	cout << "short max: " << SHRT_MAX << endl;
	cout << "short min: " << SHRT_MIN << endl;
	return 0;
}

 

 

 


bool 데이터형

  • true, false의 값을 가짐
  • boolalpha 를 cout에 넣으면 true와 false가 나옴
  • noboolalpha 를 cout에 넣으면 1과 0이 나옴 (default 값)
#include <iostream>
int main()
{
	using namespace std;
	
	bool b = true;  // 1 넣어줘도 무관 
	bool c = false; // 0 넣어줘도 무관


	cout << b <<"  " << c << endl;
	cout << boolalpha;
	cout << b << "  " << c << endl;
	cout << noboolalpha;
	cout << b << "  " << c << endl;

	return 0;
}

 

 

Comments