C++ BasicType

m._.jooong·2023년 3월 15일
0

C++

목록 보기
2/23
#include <iostream>

/** 문자열 관련 라이브러리*/
#include <string>
/** Climits 라이브러리를 추가하면 각 자료형의 촤대값과 최소값을 알 수 있다.*/
#include <climits>
/** 배열의 사이즈를 가져오기 위한 라이브러리*/
#include <array>

/** C++ 표준 라이브러리 사용*/
using namespace std;	// c++

int main()
{
	/**
	C++은 C언어의 대부분의 문법적 요소를 가지고 있다.
	C++은 Super C라고도 한다.
	*/

	/**
	기본 자료형

	bool   : Boolean
	char   : character
	int    : Integer
	float  : Floating Point
	double : Double Floating Point

	string(문자열)은 기본 자료형이 아닌 외부 라이브러리 입니다.
	*/

	/**
	전세계의 언어의 모든 문자는 문자에 해당하는 아스크 코드값이 있다.
	아스키 코드 테이블에 따라 각 문자에 해당하는 숫자(혹은 숫자에 해당하는 문자)
	를 출력할 수 있다.
	https://ko.wikipedia.org/wiki/ASCII
	*/

	cout << "아스키 코드를 숫자로 출력하기" << endl;
	cout << endl;
	for (int i = 48; i <= 57; i++)
	{
		cout << "ASCII Code" << i << " : " << (char)i << endl;
	}
	cout << endl;

	cout << "숫자를 아스키 코드로 출력" << endl;
	cout << endl;
	char charArr1[] = { '0', '1', '2', '3','4','5','6','7','8','9' };
	for (int i = 0; i < size(charArr1); i++) {
		cout << "ASCII Code " << charArr1[i] << " : " << (int)charArr1[i] << endl;
	}
	cout << endl;

	cout << "아스키 코드를 대문자로 출력" << endl;
	cout << endl;
	for (int i = 65; i <= 90; i++)
	{
		cout << "ASCII Code " << i << " : " << (char)i << endl;
	}
	cout << endl;

	cout << "대문자를 아스키코드로 출력" << endl;
	cout << endl;
	char charArr2[] = { 'A','B','C','D', 'E', 'F','G','H','I',
						'J', 'K', 'L','M','N','O','P','R','S','T','U','V','W','X','Y','Z' };
	for (int i = 0; i < size(charArr2); i++)
	{
		cout << "ASCII Code " << charArr2[i] << " : " << (int)charArr2[i] << endl;
	}
	cout << endl;


	cout << "아스키 코드를 소문자로 출력" << endl;
	cout << endl;
	for (int i = 97; i <= 122; i++)
	{
		cout << "ASCII Code" << i << " : " << (char)i << endl;
	}
	cout << endl;

	cout << "소문자를 아스키코드로 출력" << endl;
	cout << endl;
	char charArr3[] = { 'a','b','c','d', 'e', 'f','g','h','i',
						'j', 'k', 'l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' };
	for (int i = 0; i < size(charArr3); i++)
	{
		cout << "ASCII Code " << charArr3[i] << " : " << (int)charArr3[i] << endl;
	}
	cout << endl;

	int intValue1 = 'A';
	/** 문자 타입에 해당하는 아스키 코드 값을 출력해 보기.*/
	cout << "intValue1 : " << intValue1 << endl;
	/**
	아스키 코드값을 문자 타입으로 명시적 형변환 해서 출력해 보기
	(char) : 자료형을 명시적으로 변경해 주고 있다. 명시적 형변환이라고 함
	*/
	cout << "(char)intValue1 : " << (char)intValue1 << endl;
	cout << endl;

	char charValue1 = 72;
	/** 아스키 코드값에 해당하는 문자 타입으로 출력하기*/
	cout << "charValue1 : " << charValue1 << endl;
	// 문자 타입을 정수값으로 명시적 형변환해서 문자 타입에 해당하는 아스키 코드값을 출력
	cout << "(int)charValue1 : " << (int)charValue1 << endl;
	cout << endl;

	/**
	정수형 자료형

	int			 : 가장 기본이 되는 정수형 4바이트
	signed int	 : 부호가 있다는 것을 의미한다.
					signed 자료형은 음수, 0, 양수를 저장할 수 있는 자료형
	unsigned int : 부호가 없는 정수를 저장
					0과 양수만을 저장할 수 있는 자료형
	short int	 : int형보다 작거나 같은 경우, 2바이트
	long int	 : int형보다 크거나 같은 경우, 4바이트
	*/

	/**
	int타입의 변수를 선언하고 초기화 하기
	다양한 표현 방식이 있다.
	*/
	int intValue2 = 1;
	cout << "intValue2 : " << intValue2 << endl;

	int intValue3(2);
	cout << "intValue3 : " << intValue3 << endl;

	int intValue4 = { 3 };
	cout << "intValue4 : " << intValue4 << endl;

	int intValue5{ 4 };
	cout << "intValue5 : " << intValue5 << endl;
	cout << endl;

	/**
	Boolean타입
	C#이나 자바에서 Boolean 타입은 true 와 false이다.
	하지만 C++은 1 과 0이다.
	C언어때는 Boolean 타입이 없었다. C언어와의 호환성을 위해
	C++은 1, 0이다

	조건 ? A문장 : B문장		삼항연산자
	조건에 맞으면 A문장이 실행되고, 틀리면 B문장 실행된다.
	*/
	bool boolValue1 = true;
	cout << "boolValue1 : " << boolValue1 << endl;
	cout << "boolValue1 : " << (boolValue1 ? 1 : 0) << endl;
	cout << "boolValue1 : " << (boolValue1 ? "true" : "false") << endl;
	cout << endl;

	bool boolValue2 = false;
	cout << "boolValue2 : " << boolValue2 << endl;
	cout << "boolValue2 : " << (boolValue2 ? 1 : 0) << endl;
	cout << "boolValue2 : " << (boolValue2 ? "true" : "false") << endl;
	cout << endl;

	/**
	부동소수(실수) 자료형

	float		: 게임산업에서는 float만 사용한다.
					게임은 정밀도를 코딩하는 것이 아니고, 재미를 코딩함
	double		: 게임 이외에 double타입 사용
	long double

	실수 표현의 정밀도 측면에서 float를 single precistion, double을 double
	pricision 이라고 한다. double은 float의 두배를 사용하기 때문에 
	double이라고 함
	float와 double의 차이는 메모리를 얼마나 사용하는지에 따라 다르다.
	float는 int와 똑같은 사이즈인 4바이트로 32비트(4 X 8Byte)를 사용한다.
	double은 8바이트로 64비트를 사용한다.
	float 타입은 저장될 값 뒤에 f를 사용한다. f를 붙이지 않으면 오류발생.
	double은 정밀도가 double precsion으로 높기 때문에 실수 그대로 표현 가능하다.

	*/

	/**
	클라이언트 프로그래머
	엔진 프로그래머
	서버 프로그래머
	*/

	float floatValue1 = 3.14f;
	cout << "floatValue1 : " << floatValue1 << endl;
	cout << endl;

	double doubleValue1 = 3.14;
	cout << "doubleValue1 : " << doubleValue1 << endl;
	cout << endl;

	/** 
	저장공간의 크기를 확인할 수 있다.
	sizeof 연산자를 사용하면 자료형, 변수, 상수, 클래스의 크기 바이트 단위로
	알 수 있다.
	*/
	cout << "bool size : " << sizeof(bool) << " Byte" << endl;
	cout << endl;

	cout << "int size : " << sizeof(int) << " Byte" << endl;
	cout << "short size : " << sizeof(short int) << " Byte" << endl;
	cout << "long size : " << sizeof(long int) << " Byte" << endl;
	cout << endl;

	cout << "char size : " << sizeof(char) << " Byte" << endl;
	cout << endl;

	cout << "string size : " << sizeof(string) << " Byte" << endl;
	cout << endl;

	cout << "float size : " << sizeof(float) << " Byte" << endl;
	cout << "double size : " << sizeof(double) << " Byte" << endl;
	cout << "long double size : " << sizeof(long double) << " Byte" << endl;
	cout << endl;

	/**
	변수의 범위
	climit 라이브러리를 전처리기에 포함시키면 각 자료형의 최대값과 최소값을 알 수 있다.
	#include <climis>
	*/
	cout << "char자료형의 최소값 : " << SCHAR_MIN << endl;
	cout << "char자료형의 최대값 : " << SCHAR_MAX << endl;
	cout << endl;

	cout << "short자료형의 최소값 : " << SHRT_MIN << endl;
	cout << "short자료형의 최소값 : " << SHRT_MAX << endl;
	cout << endl;

	cout << "int자료형의 최소값 : " << INT_MIN << endl;
	cout << "int자료형의 최소값 : " << INT_MAX << endl;
	cout << endl;

	cout << "long자료형의 최소값 : " << LONG_MIN << endl;
	cout << "long자료형의 최대값 : " << LONG_MAX << endl;
	cout << endl;

	cout << "long long자료형의 최소값 : " << LLONG_MIN << endl;
	cout << "long long자료형의 최소값 : " << LLONG_MAX<< endl;
	cout << endl;


}

0개의 댓글