💻 개인공부 💻/C | C++

[C++] C++에서의 데이터타입, signed와 unsigned

공대생 배기웅 2020. 11. 8. 23:52
반응형

데이터 타입

 

▶ C++에서의 데이터타입은 아래 표와 같다.

 

 

 

▶ 코딩을 이용하여 크기를 알아보자. 소스코드는 아래와 같다.

 

#include<iostream>
using namespace std;

int main() {
	unsigned short _short;
	int _int;
	long _long;
	char _char;
	float _float;
	double _double;
	long double _longDouble;
	bool _bool;
	cout << "size of _short : " << sizeof _short << endl;
	cout << "size of _int : " << sizeof _int << endl;
	cout << "size of _long : " << sizeof _long << endl;
	cout << "size of _char : " << sizeof _char << endl;
	cout << "size of _float : " << sizeof _float << endl;
	cout << "size of _double : " << sizeof _double << endl;
	cout << "size of _longDouble : " << sizeof _longDouble << endl;
	cout << "size of _bool : " << sizeof _bool << endl;

}

 

 

 

 

 

signed vs unsigned

 

▶ signed와 unsigned의 차이는 부호의 유무이다. signed는 부호를 표시하겠다, 즉 음수까지 포함한다는 의미이고, unsigned는 부호를 표시하지 않기 때문에 양수만을 포함한다. 그러나 unsigned는 음수를 표현못하는 대신, signed가 표현할 수 있는 양수의 개수의 2배까지 나타낼 수 있다. 

 

 

 

#include<iostream>
using namespace std;

int main() {
	short val1 = 0;
	unsigned short val2 = 0;

	val1 = val1 - 1;
	val2 = val2 - 1;

	cout << "signed short is" << val1 << endl; //-1
	cout << "unsigned short is " << val2 << endl; //65535
}

 

 

 

 

 

const 제한자

 

▶ 초기화된 변수에 제한자를 붙이면 그 변수에는 다른 값을 대입할 수 없다.

 

 

 

 

▶ 위의 사진을 보면 컴파일 오류가 발생했음을 알 수 있는데 그 이유는 seven은 수정이 불가능한 변수이기 때문이다. const 제한자가 붙였기 때문에 수정이 불가능하다.

728x90
반응형