1bit
은 0이나 1이 들어갈 수 있는 한 칸float
은 32bits, double
은 64bitsunsigned
는 음수가 없는 자료형#include <iostream>
int main()
{
using namespace std;
//bool bValue = true;
bool bValue = false;
cout << (bValue ? 1 : 0 )<< endl;
char chValue = 'A';
cout << chValue << endl;
cout << (int)chValue << endl;
chValue = 65;
cout << chValue << endl;
float fValue = 3.141592f; // f를 안쓰면 수를 double 자료형으로 인식해서 컴파일 에러
double dValue = 3.141592;
cout << fValue << endl;
cout << dValue << endl;
//modern c++에 있는 기능
auto aValue = 3.141592;
auto aValue2 = 3.141592f;
cout << aValue << endl;
cout << sizeof(aValue) << endl;
cout << aValue2 << endl;
cout << sizeof(aValue2) << endl;
return 0;
}
출력결과
0
A
65
A
3.14159
3.14159
3.14159
8
3.14159
4
copy initialization
direct initialization
uniform initailization
int k = 0, l((int)456.2), m{ 123 };
영역 Category | 형 Type | 최소 크기 | 기타 |
---|---|---|---|
문자 Character | Char | 1 바이트 | |
정수 Integer | short | 2바이트 | |
int | 2바이트 | 대부분 4바이트 | |
long | 4바이트 | ||
long long | 8 바이트 | C99/C++11 type |
#include <iostream>
#include <cmath> // pow
#include <limits> // max, min lowest
int main()
{
using namespace std;
short s = 1; // 2bytes = 2 * 8 bits = 16 bits
cout << pow(2, sizeof(short) * 8 - 1) -1 << endl;
cout << numeric_limits<short>::max() << endl;
cout << std::numeric_limits<short>::min() << endl;
cout << std::numeric_limits<short>::lowest() << endl;
return 0;
}
출력 결과
32767
32767
-32768
-32768
s = 32767;
s += 1; // 32768
cout << s << endl; // overflow
s = std::numeric_limits<short>::min();
s -= 1; // -32769
cout << s << endl; // overflow
출력 결과
-32768
32767
✍ overflow가 발생하여 저장하려던 값과 다른 값이 저장됨을 알 수 있다.
cstdint
를 사용해야 한다.iostream
을 헤더파일로 사용할 경우에는 굳이 cstdint
를 호출할 필요가 없다.#include <iostream>
int main()
{
using namespace std;
std::int16_t i(5); // 16bit 데이터(short)로 바꿔준다.
std::int8_t myint = 65; // 8bit 데이터(char)로 바꿔준다.
cout << myint << endl;
std::int_fast8_t fi(5); // 8bit 데이터 중 가장 빠른 자료형으로 선언한다
std::int_least64_t fl(5); // 적어도 64bit를 갖는 자료형으로 선언한다.
return 0;
}
#include <iostream>
void my_function()
{
}
int main()
{
//void my_void;
int i = 123;
float f = 123.456f;
void *my_void;
// 포인터 주소의 타입은 서로 같기 때문에 void 형식으로 캐스팅 가능
my_void = (void*)&i;
my_void = (void*)&f;
return 0;
}
영역 Category | 형 Type | 최소 크기 | 전형적인 크기 |
---|---|---|---|
부동소수점 Floating point | float | 4 바이트 | 4 바이트 |
double | 8 바이트 | 8, 12 or 16 바이트 | |
long double | 8 바이트 | 8 바이트 |
float
의 경우 32 bit 중 1bit은 부호, 8bit은 지수, 나머지는 가수(0~1사이) 수 표현에 사용된다.<Code>
cout << numeric_limits<float>::max() << endl;
cout << numeric_limits<float>::lowest() << endl;
출력결과
3.40282e+38
-3.40282e+38
✎ float 자료형의 표현 범위
float f(123456789.0f); // 10 significant digits
cout << std::setprecision(9);
cout << f << endl;
double d(0.1); // 10 significant digits
cout << d << endl;
cout << std::setprecision(17);
cout << d << endl;
double d1(1.0);
double d2(0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1);
cout << setprecision(17);
cout << d1 << endl;
cout << d2 << endl;
출력결과
1234567920.1
0.100000001490116121
0.99999999999999989
예상했던 것과 다른 결과물을 얻을 수 있다.
bool은 0을 제외한 다른 모든 수(문자)는 true
로 판단한다.
bool 결과를 1, 0말고 true, false로 보기
- cout << std::boolalpha
- 해제는 std::noboolalpha
논리연산자
- && : AND 연산
- || : OR 연산
- ! : not
if문
if (조건문)
{
조건문이 true일 때 실행
}
else
{
조건문이 false일 때 실행
}
#include <iostream>
bool isEqual(int a, int b)
{
bool result = (a == b);
return result;
}
int main()
{
using namespace std;
bool b1 = true; // copy initialization
bool b2(false); // direct '''
bool b3{ true }; // uniform ini
b3 = false;
cout << std::boolalpha;
cout << b3 << endl;
cout << !true << endl;
cout << (true || false) << endl;
cout << (false && true) << endl;
if (isEqual(3, 1))
{
cout << "This is True " << endl;
}
else
{
cout << "This is False " << endl;
}
return 0;
}
출력결과
false
false
true
false
This is False
char c = "Hello, world";
char c = 'A';
char c1(65);
char c2('A');
cout << c1 << " " << c2 << " " << int(c1) << " " << int(c2) << endl;
출력결과
A A 65 65
//C-style casting
cout << (char)65 << endl;
cout << (int)'A' << endl;
//cpp style casting
cout << char(65) << endl;
cout << int('A') << endl;
cout << static_cast<char>(65) << endl;
cout << static_cast<int>('A') << endl;
출력결과
A
65
A
65
A
65
int x = 012
➔ 10int x = 0x12
➔ 18int x = 0b1010
➔ 100b101111111010
= 0b1011'1111'1010
const
를 붙인다const int a = 10
= int const a = 10