uint8_t, uint16_t, uint32_t, uint64_t vs int8_t, int16_t, int32_t, int64_t, bool, char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long, long long, unsigned long long, float, double long, double wchar_t 정리
1. uint8_t, uint16_t, uint32_t, uint64_t vs int8_t, int16_t, int32_t, int64_t는 왜 사용하는 것인가?
C언어의 기본 자료형으로 char, short, int, long 등이 있지만 몇비트 머신이냐에 따라 모호하기 때문에, 비트가 명확하게 나타난 stdint.h라이브러리의 자료형 타입을 자주 사용한다.
사용하기 위해 헤더파일을 입력해준다.
#include <stdio.h>
#include <stdint.h>
32비트 머신에서 int8_t = char, uint8_t = unsigned char, int16_t = short, uint16_t = unsigned short, int32_t = int, uint32_t = unsigned int와 같은 자료형이 되는 것이다.
int(number)_t 는 부호가 있는 int를 의미한다. (( -최대값 / 2 ) ~ (+최대 값 / 2))
uint(number) _t는 부호가 없는 unsigned를 나타내는 u가 앞에 붙어있다. (0 ~ 최대값)
2. 각 타입별 최대 최소 값은 아래 표와 같고 더 자세한 내용은 stdint.h 헤더파일에 정의되어 있다.
자료형 | 저장할 수 있는 값의 범위 | 크기 |
uint8_t | 0 ~ 255 | 8bit |
uint16_t | 0 ~ 65,535 | 16bit |
uint32_t | 0 ~ 4,294,967,295 | 32bit |
uint64_t | 0 ~ 18,446,744,073,709,551,615 (1.8446744e+19) |
64bit |
int8_t | -128 ~ 127 | 8bit |
int16_t | -32,768 ~ 32,767 | 16bit |
int32_t | -2,147,483,648 ~ 2,147,483,647 | 32bit |
int64_t | -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807 |
64bit |
3. 32비트 및 64비트 컴파일러에서 나머지 타입들의 데이터 형식 범위
자료형 | 저장할 수 있는 값의 범위 | 크기 |
bool | false, true | 1bit |
char | -128 ~ 127 | 8bit |
unsigned char | 0 ~ 255 | 8bit |
short | -32,768 ~ 32,767 | 16bit |
unsigned short | 0 ~ 65,535 | 16bit |
int | MIN: -32,768 ~ 32,767 MAX: -2,147,483,648 ~ 2,147,483,647 |
32bit |
unsigned int | 0 ~ 4,294,967,295 | 32bit |
long | MIN: -2,147,483,648 ~ 2,147,483,647 MAX: -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807 |
32bit 64bit |
unsigned long | MIN: 0 ~ 4,294,967,295 MAX: 0 ~ 18,446,744,073,709,551,615 (1.8446744e+19) |
32bit 64bit |
long long | -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807 |
64bit |
unsigned long long | 0 ~ 18,446,744,073,709,551,615 (1.8446744e+19) |
64bit |
float | -3.4*10^38 ~ 3.4*10^38 (3.4E+/-38(7개의 자릿수), 정밀도 7자리) |
32bit |
double | -1.8*10^308 ~ 1.8*10^308 (1.7E+/-308(15개의 자릿수), 정밀도 15자리) |
64bit |
long double | -1.8*10^308 ~ 1.8*10^308 (1.7E+/-308(15개의 자릿수) ,정밀도 15자리) |
64bit |
wchar_t | 0 ~ 65,535 | 16bit |
위 표에서 int의 범위는 아래와 같다.
32비트 컴파일러: int의 범위가 MIN
64비트 컴파일러: int의 범위가 MAX
또한 long에 대한 범위의 변화는 아래와 같다.
Windows(32비트, 64비트), MAC(32비트) 컴파일러: long의 범위가 MIN
Windows(64비트) 컴파일러: long의 범위가 MAX
long은 OS와 어플리케이션의 bit에 따라 달라지기때문에 크로스플랫폼 개발시 long보다는 stdint.h에 포함된 int32_t, uint32_t등을 사용하는 것을 권장한다.
달라지는 내용은 아래와 같다.
OS | Application Bit (binary): long size |
Windows (x86-64) | 32bit: 4byte 64bit: 4byte |
OS X (x86-64) | 32bit: 4byte 64bit: 8byte |
Linux (x86-64) | 32bit: 4byte 64bit: 8byte |
Aix (PowerPC) | 32bit: 4byte 64bit: 8byte |
HP_UX (IA-64) | 32bit: 4byte 64bit: 8byte |
Solaris (Sparc) | 32bit: 4byte 64bit: 8byte |
'언어 > C, C++' 카테고리의 다른 글
[C++] #define, constexpr, PROGEM 비교 (0) | 2024.10.27 |
---|---|
[C++] PROGEM (0) | 2024.10.26 |
[C++] constexpr (0) | 2024.10.25 |
[C++] #define (0) | 2024.10.24 |
[C++] constexpr이란? const와 constexpr의 차이 ( const vs constexpr ) (0) | 2024.01.08 |
댓글