본문 바로가기

강의자료/C/C++

010. 자료형(타입)에 따른 메모리의 크기와 한계값

◈ 이 글은 강의를 위하여 제가 직접 작성한 내용입니다. 따라서 퍼가실 경우, 출처를 명확히 해주시기 바랍니다!!
◈ This ariticle was written by me for teaching. So if you want to copy this article, please clarify the source!!


변수를 선언할 때 사용하는 자료형의 크기가 어느정도인지, 저장할 수 있는 한계값은 어느정도인지 알아보자.
#include  
using namespace std;

//enum
enum GAME {SCISSORS, ROCK, KERCHIEF};

//sizeof를 이용한 기본타입의 크기  
int main( )
{
	cout << "/---------size ------ byte ------/" << endl;
	cout << "short              :   " << sizeof(short) << endl;
	cout << "long               :   " << sizeof(long) << endl;
	cout << "int                :   " << sizeof(int) << endl;
	cout << "unsigned  int      :   " << sizeof(unsigned int) << endl;
	cout << "char               :   " << sizeof(char) << endl;
	cout << "wchar_t            :   " << sizeof(wchar_t) << endl;
	cout << "float              :   " << sizeof(float) << endl;
	cout << "double             :   " << sizeof(double) << endl;
	cout << "enum GAME          :   " << sizeof(GAME) << endl;

	char c = 'c';
	int n = 32;

	cout << "char 변수          :   " << sizeof(c) << endl;
	cout << "int  변수          :   " << sizeof(n) << endl;

	return 0;
}
#include  
#include 
using namespace std;

#define nl numeric_limits

int main()
{
	//numeric_limits::max()
	//numeric_limits::min()

	int maxI=nl::max();
	int minI=numeric_limits::min();

	unsigned int maxUI=numeric_limits::max();
	unsigned int minUI=numeric_limits::min();

	int maxWC=numeric_limits::max();
	int minWC=numeric_limits::min();

	float maxF=numeric_limits::max();
	float minF=numeric_limits::min();
	double maxD=numeric_limits::max();
	double minD=numeric_limits::min();	

	cout << "/--------- numeric_limits<> 메서드 --------/" << endl;
	cout << "int      :   max:" << maxI << "  min: " << minI << endl;
	cout << "uint     :   max:" << maxUI << " min: " << minUI << endl;
	cout << "wchar_t  :   max:" << maxWC << "  min: " << minWC << endl;
	cout << "float    :   max:" << maxF << "  min: " << minF << endl;
	cout << "double   :   max:" << maxD << "  min: " << minD << endl << endl;

	cout << "/--------- define 정의 --------/" << endl;
	cout << "int      :   max:" << INT_MAX << "  min: " << INT_MIN << endl;
	cout << "long     :   max:" << LONG_MAX << "  min: " << INT_MIN << endl;
	cout << "float    :   max:" << FLT_MAX << "  min: " << FLT_MIN << endl;
	cout << "double   :   max:" << DBL_MAX << "  min: " << DBL_MIN << endl;

	return 0;
}