반응형
// boolean
#include <iostream>
int main() {
using namespace std;
bool bValue = true; // true(1), false(0)
cout << (bValue ? 0 : 1) << '\n'; // bValue가 true(1)이기 때문에, '0' 출력
return 0;
}
// 문자형 char
#include <iostream>
int main() {
using namespace std;
char chValue = 'A';
char chValue2 = 65;
cout << chValue << '\n'; // 'A' 출력
cout << (int)chValue << '\n'; // '65' 출력
cout << chValue2 << '\n'; // 'A' 출력
return 0;
}
// 실수형 float, double
#include <iostream>
int main() {
using namespace std;
float fValue = 3.141592f;
double dValue = 3.141592;
cout << fValue << '\n'; // '3.14159' 출력 (소숫점 절삭)
cout << dValue << '\n'; // '3.14159' 출력 (소숫점 절삭)
cout << sizeof(fValue) << '\n'; // 4 출력(float 타입의 메모리 크기는 4byte 이다.)
cout << sizeof(dValue) << '\n'; // 8 출력(double 타입의 메모리 크기는 8byte 이다.)
return 0;
}
// auto: 자료형 타입을 미리 선언하지 않고, 변수값에 따라 자료형이 정해진다.
#include <iostream>
int main() {
using namespace std;
auto A = 1;
auto B = 3.141592f;
auto C = 'B';
cout << sizeof(A) << '\n'; // '4' 출력, int
cout << sizeof(B) << '\n'; // '4' 출력, float
cout << sizeof(C) << '\n'; // '1' 출력, char
return 0;
}
참고: Inflearn, 홍정모의 따라하며 배우는 C++, '2.1 기본 자료형 소개'
반응형
'C++' 카테고리의 다른 글
[C++] 자료형의 크기와 범위 (논리, 자료, 정수) (0) | 2021.09.27 |
---|---|
[C++] 변수 초기화 (0) | 2021.09.22 |
[C++] 헤더 가드란? (0) | 2021.09.10 |
[C++] 소스/헤더 파일 만들기 (0) | 2021.09.08 |
[C++] 함수의 선언 및 정의 (0) | 2021.08.20 |