[C++] 자료형과 범위 – sizeof

C++에서 사용하는 데이터 형식의 종류(자료형)와 그 값의 범위는 아래와 같습니다.

해당 값은 아래 코드와 같이 sizeof로 확인이 가능합니다.

#include <iostream>
using namespace std;

int main()
{
	cout << "char : " << sizeof(char) << endl;
	cout << "unsigned char : " << sizeof(unsigned char) << endl;
	cout << "short : " << sizeof(short) << endl;
	cout << "unsigned short : " << sizeof(unsigned short) << endl;
	cout << "int : " << sizeof(int) << endl;
	cout << "unsigned int : " << sizeof(unsigned int) << endl;
	cout << "long : " << sizeof(long) << endl;
	cout << "unsigned long : " << sizeof(unsigned long) << endl;
	cout << "long long : " << sizeof(long long) << endl;
	cout << "unsigned long long : " << sizeof(unsigned long long) << endl;
	cout << "float : " << sizeof(float) << endl;
	cout << "double : " << sizeof(double) << endl;

	return 0;
}

일반적으로 데이터를 표현할 때 데이터 비트 수가 n개이면 2의 n승까지 데이터 표현이 가능합니다.
예를 들어 unsigned char의 경우 8bit(=1Byte)이기 때문에 2의 8승, 즉 0 ~ 255까지 표현이 가능한 것입니다.

하지만 부호가 있는 signed 자료형을 표현할 때는, 아래 그림과 같이 비트 1개를 부호로 표시하기 때문에 -(2의 n-1승) ~ +(2의 n-1승) -1범위만큼 표현이 가능합니다

ex) signed char : -128 ~ 127

MSDN 링크도 참고 바랍니다.