Programming/C++

[c++] bool

OKOK 2017. 5. 12. 01:02

#include<iostream>

using namespace std;


int main(void)

{

int num = 10;

int i = 0;


cout << "true: " << true << endl;

cout << "false: " << false << endl;


while (true)

{

cout << i++ << ' ';

if (i > num)

break;

}

cout << endl;


cout << "sizeof 1: " << sizeof(1) << endl;

cout << "sizeof 0: " << sizeof(0) << endl;

cout << "sizeof true:" << sizeof(true) << endl;

cout << "szie of false: " << sizeof(false) << endl;

return 0;

}


* true, false 콘솔에 출력했을 때 출력 내용

* 무한루프 활용하기 위해 1과 true 사용가능

* 상수 1, 0의 데이터 크기 확인

* true, false 크기 확인 1바이트 크기



#include<iostream>

using namespace std;



bool IsPositive(int num)

{

if (num <= 0)

return false;

else

return true;

}


int main(void)

{

bool isPos;

int num;

cout << "Input number: ";

cin >> num;


isPos = IsPositive(num);

if (isPos)

cout << "Positive number" << endl;

else

cout << "Negative number" << endl;


return 0;

}



* bool 이라는 자료형 true, false 를 데이터로 저장가능하다. 다른 자료형과 동일하게 사용한다.