Notice
Recent Posts
Recent Comments
Link
250x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- Windows10
- Computer Vision
- error
- 프로세스
- 공부
- 시스템프로그래밍
- 리눅스
- 알고리즘
- TensorFlow
- linux
- 운영체제
- 회귀
- C++
- 백준알고리즘
- shell
- 학습
- 턱걸이
- c
- Windows 10
- 코딩
- CV
- 딥러닝
- 백준
- C언어
- 프로그래밍
- OpenCV
- python
- 영상처리
- 쉘
- 텐서플로우
Archives
- Today
- Total
줘이리의 인생적기
12. [C++] 불리언자료형(boolean) 본문
728x90
불리언 자료형에 대해서 알아보도록 하겠습니다.
true냐 false냐를 표현할 수 있는 자료형이죠
#include <iostream>
using namespace std;
int main() {
bool b1 = true;
bool b2 = false;
cout << noboolalpha;
cout << b1 << endl;
cout << b2 << endl;
cout << boolalpha;
cout << ! b1 << endl;
cout << ! b2 << endl;
return 0;
}
noboolalpha, boolalpha를 통해서 0과 1로 표현을 할 것이냐 true와 false로 표현할 것이냐 정해줄 수 있습니다.
! 는 not의 의미이기 때문에 true와 false가 바뀐 것을 알 수 있습니다.
간단하게 and연산과 or연산도 해보겠습니다.
#include <iostream>
using namespace std;
int main() {
cout << boolalpha;
cout << (true && true) << endl;
cout << (true && false) << endl;
cout << (false && false) << endl;
cout << (false && true) << endl;
cout << (true || true) << endl;
cout << (true || false) << endl;
cout << (false || false) << endl;
cout << (false || true) << endl;
return 0;
}
둘 다 true일 때만 true인 and연산자, 둘 중 하나라도 true일 때 true인 or연산자도 해보았습니다.
주의해야할 점이 하나 있습니다
다음 코드에서 input값을 true라고 입력했을 때 출력 값은 무엇일까요?
#include <iostream>
using namespace std;
int main() {
bool b;
cin >> b;
cout << "input = " << b << endl;
return 0;
}
true를 입력하면 false라고 출력이 됩니다.
c++ 버전마다 다르다고 하니 이렇게 문제가 생길 수 있는 코딩은 주의해야 하겠습니다.
'공부 > C++' 카테고리의 다른 글
11. [C++] 부동소수점(float) (0) | 2021.09.24 |
---|---|
10. [C++] 자료형 크기, 범위(sizeof, numeric_limits) (0) | 2021.09.17 |
09. [C++] 전처리기(ifndef, ifdef, endif) (0) | 2021.09.03 |
08. [C++] 헤더가드 (0) | 2021.09.01 |
07. [C++] 헤더파일(.h) (0) | 2021.08.30 |