C/C++
Data Types
Wanho Choi
Bit / Byte
C/C++ : Data Types
•
여러 가지 자료형을 사용하는 이유‣
정수와 실수의 데이터 표현 방식이 다르기 때문‣
메모리를 효율적으로 사용하기 위해서Example: 8 Bit Signed Integer
Int8 0 0 0 1 0 0 0 Sign Bit Value Bit = 4•
Sign-Magnitude form‣
가장 왼쪽 비트: 부호(0: +, 1: -)‣
나머지 비트들: 크기‣
0 표현의 중복: +0, -0•
1’s complement form:‣
가장 왼쪽 비트: 부호(0: +, 1: -)‣
음수일 때 보수로 표현‣
여전히 0 표현의 중복: +0, -0•
2’s complement form:‣
가장 왼쪽 비트: 부호(0: +, 1: -)‣
음수일 때 보수+1로 표현‣
0 표현의 중복이 없음C/C++ : Data Types & Sizes
•
64 bit OS 기준Type Name Bytes
char 1 short 2 int 4 unsigned int size_t 8 long 8 long long 8 float 4 double 8 long double 16
Type Name Description
int8_t 1 byte signed integer
int16_t 2 byte signed integer
int32_t 4 bytes signed integer
int64_t 8 byte signed integer
uint8_t 1 byte unsigned integer
uint16_t 2 byte unsigned integer
uint32_t 4 bytes unsigned integer
C/C++ : Data Types & Ranges
•
64 bit OS 기준bytes (bits)
range limit
min max min max char 1 (8) -128 (= 27) 127 (= 27-1) CHAR_MIN
-128 CHAR_MAX 127 unsigned char 1 (8) 0 255 (= 28-1) - UCHAR_MAX
255 short 2 (16) -32,768 (= 215) 32,767 (= 215-1) SHRT_MIN
-32,768 SHRT_MAX 32,767 unsigned short 2 (16) 0 65,535 (= 216-1) - USHRT_MAX
65,535 int 4 (32) -2,147,483,648 (= 231) 2,147,483,647 (= 231-1) INT_MIN
-2,147,483,648 2,147,483,647INT_MAX unsigned int 4 (32) 0 4,294,967,295 (= 232-1) - UINT_MAX
4,294,967,295 float 4 (32) -3.4 × 1038 3.4 × 1038 FLT_MIN
1.17549e-38 3.40282e+38FLT_MAX double 8 (64) -1.79 × 10308 1.79 × 10308 DBL_MIN
2.22507e-308 1.79769e+308DBL_MAX
Constant
•
Symbolic constant‣
이름을 가지는 변수 예) const double PI = 3.14;•
Literal constant‣
변수 이름이 없는 상수 예) int a = 10 + 20; 에서 10과 20‣
연산을 위해서 사용되는 이러한 수들도 메모리에 저장되어야 한다.‣
데이터형에 대한 명시가 없다면•
정수와 문자는 기본적으로 int형, 실수는 기본적으로 double형으로 자료형이 결정된다.‣
접미사(suffix)를 사용해서 자료형을 명시적으로(explicitly) 지정할 수도 있다.•
123u 또는 123U: unsigned int•
123l 또는 123L: long int•
123ul 또는 123UL: unsigned long intImplicit / Explicit Type Conversion
•
Implicit type conversion 예) int a = 1.23 + 3.14;‣
사용자가 개입하지 않아도 자동으로 자료형이 변환되는 것‣
큰 자료형에서 작은 자료형으로 변환시 데이터 손실이 발생할 수 있다.‣
위의 예의 경우 double형에서 int형으로 변환되면서 데이터 손실 발생!•
Explicit type conversion 예) float a = (float)100.0;‣
이와 같이 자료형을 명시해서 자료형을 변환하는 것‣
역시 큰 자료형에서 작은 자료형으로 변환시 데이터 손실이 발생할 수 있다.Overflow / Underflow
•
Overflow‣
위로 넘친다는 의미‣
처리할 수 있는 가장 큰 값보다 더 큰 값을 다루는 경우‣
INT_MAX + 1 = INT_MIN•
Underflow‣
아래로 넘친다는 의미‣
처리할 수 있는 가장 작은 값보다 더 작은 값을 다루는 경우‣
INT_MIN - 1 = INT_MAX‣
The extended space is filled with the the most significant bit.0 0 1 1 0 0 0 1 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0
Integer Promotion
•
short형 끼리 계산을 했고, overflow도 아닌데 int형으로 자동 형변환이 이루어졌음•
크기가 4 bytes 보다 작은 값들을 연산할 때에는 자동적으로 4 bytes int형으로 변환되어 계산이 수행됨•
CPU가 효율적으로 처리하기 좋기 때문short a = 1, b =2;
cout << sizeof(a + b) << endl; // 4
double a = 123 + 3.14;
•
다른 자료형들끼리는 연산이 불가능함•
따라서 자동 형변환이 이루어지는데, 이 때 손실을 최소화하는 방향으로 형변환이 일어남Frequently Asked Question
cout << 25u - 50 << endl;
•
두 자료형이 다르기 때문에 둘 중 하나로 자료형이 통일되어야 함•
“lower type”은 “higher type”으로 promoted됨•
Type hierarchy‣
(highest) long double, double, float, unsigned long int, long int, unsigned int, int (lowest)•
따라서, 여기서는 unsigned int형으로 자료형이 통일됨‣
unsigned int: higher type‣
int: lower type•
그러므로 50은 50u로 promoted됨다음 구조체의 크기는?
struct A { int x; // 4 bytes char y; // 1 byte }; struct C { char x; // 1 byte int y; // 4 bytes char z; // 1 byte }; struct B { char x; // 1 byte char y; // 1 byte int z; // 4 bytes };다음 구조체의 크기는?
struct A { int x; // 4 bytes char y; // 1 byte }; struct C { char x; // 1 byte int y; // 4 bytes char z; // 1 byte }; struct B { char x; // 1 byte char y; // 1 byte int z; // 4 bytes };다음 구조체의 크기는?
5 bytes
6 bytes
6 bytes
8 bytes
8 bytes
12 bytes
struct A { int x; // 4 bytes char y; // 1 byte }; struct C { char x; // 1 byte int y; // 4 bytes char z; // 1 byte }; struct B { char x; // 1 byte char y; // 1 byte int z; // 4 bytes };