줘이리의 인생적기

10. [C++] 자료형 크기, 범위(sizeof, numeric_limits) 본문

공부/C++

10. [C++] 자료형 크기, 범위(sizeof, numeric_limits)

줘이리 2021. 9. 17. 23:00
728x90

 

자료형의 크기와 범위를 알아보도록 하겠습니다.

 

크기는 sizeof로 알 수 있고,

범위는 numeric_limits를 통해 알 수 있습니다.

 

numeric_limits는 #include limits를 포함해야 사용할 수 있습니다.

 

예제를 통해 정수형부터 알아보도록 하겠습니다.

 

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

int main() {

	int a;
	short a_1;
	long a_2;
	long long a_3;

	cout << "int size \t= " << sizeof(a) << "바이트" << endl;
	cout << "int max \t= " << numeric_limits<int>::max() << endl;
	cout << "int min \t= " << numeric_limits<int>::min() << endl;

	cout << "short size \t= " << sizeof(a_1) << "바이트" << endl;
	cout << "short max \t= " << numeric_limits<short>::max() << endl;
	cout << "short min \t= " << numeric_limits<short>::min() << endl;

	cout << "long size \t= " << sizeof(a_2) << "바이트" << endl;
	cout << "long max \t= " << numeric_limits<long>::max() << endl;
	cout << "long min \t= " << numeric_limits<long>::min() << endl;
	
	cout << "long long size \t= " << sizeof(a_3) << "바이트" << endl;
	cout << "long long max \t= " << numeric_limits<long long>::max() << endl;
	cout << "long long min \t= " << numeric_limits<long long>::min() << endl;
	
	return 0;
}

문득 max값을 넘어가는 숫자가 더해지거나,

min값을 넘어가는 숫자가 빼졌을 때 어떻게 되는지 궁금해졌습니다.

 

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

int main() {

	int a = 2147483647;
	cout << a << endl;
	a++;
	cout << a << endl;
	a++;
	cout << a << endl;

	int b = -2147483648;
	cout << b << endl;
	b--;
	cout << b << endl;
	b--;
	cout << b << endl;
	return 0;

}

오.. 순환(?)되는 방식입니다.

제일 큰값에서 1을 더하니 제일 작은 값이 되고 반대도 마찬가지네요.

 


실수형의 크기와 범위에 대해서 알아보겠습니다.

 

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

int main() {
	float b;
	double c;

	cout << "float size \t= " << sizeof(b) << "바이트" << endl;
	cout << "float max \t= " << numeric_limits<float>::max() << endl;
	cout << "float min \t= " << numeric_limits<float>::min() << endl;

	cout << "double size \t= " << sizeof(c) << "바이트" << endl;
	cout << "double max \t= " << numeric_limits<double>::max() << endl;
	cout << "double min \t= " << numeric_limits<double>::min() << endl;

	return 0;
}

 


문자형에 대해서 알아보겠습니다.

 

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

int main() {
	char d = 'a';
	cout << "char size \t= " << sizeof(d) << "바이트" << endl;
	cout << "char max \t= " << numeric_limits<char>::max() << endl;
	cout << "char min \t= " << numeric_limits<char>::min() << endl;

	return 0;
}

ASCII 코드라서 출력이 안되는군요

정수형으로 형변환 하도록 하겠습니다.

 

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

int main() {
	char d = 'a';
	cout << "char size \t= " << sizeof(d) << "바이트" << endl;
	cout << "char max \t= " << (int)numeric_limits<char>::max() << endl;
	cout << "char min \t= " << (int)numeric_limits<char>::min() << endl;

	return 0;
}

'공부 > C++' 카테고리의 다른 글

12. [C++] 불리언자료형(boolean)  (0) 2021.11.01
11. [C++] 부동소수점(float)  (0) 2021.09.24
09. [C++] 전처리기(ifndef, ifdef, endif)  (0) 2021.09.03
08. [C++] 헤더가드  (0) 2021.09.01
07. [C++] 헤더파일(.h)  (0) 2021.08.30