Notice
Recent Posts
Recent Comments
Link
250x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- TensorFlow
- C언어
- 프로그래밍
- Windows 10
- 백준
- 운영체제
- C++
- OpenCV
- 딥러닝
- 쉘
- c
- 영상처리
- CV
- 공부
- shell
- 텐서플로우
- 백준알고리즘
- 시스템프로그래밍
- 턱걸이
- 회귀
- Computer Vision
- python
- linux
- 알고리즘
- error
- Windows10
- 프로세스
- 코딩
- 리눅스
- 학습
Archives
- Today
- Total
줘이리의 인생적기
10. [C++] 자료형 크기, 범위(sizeof, numeric_limits) 본문
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 |