C++ Chapter 02. C언어 기반의 C++ 2.


02-2 새로운 자료형 bool


* '참'을 의미하는 true와 '거짓'을 의미하는 false

// true와 false는 각각 1과 0을 의미하는 키워드가 아니다

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
#include <iostream>
using namespace std;
 
int main(void)
{
    int num=10;
    int i=0;
 
    cout<<"true: "<<true<<endl; // 1
    cout<<"false: "<<false<<endl; // 0
 
    while(true)
    {
        cout<<i++<<' ';
        if(i>num)
            break;
    }
    cout<<endl; // 0 1 2 3 4 5 6 7 8 9 10
 
    cout<<"sizeof 1: "<<sizeof(1)<<endl; // 4
    cout<<"sizeof 0: "<<sizeof(0)<<endl; // 4
    cout<<"sizeof true: "<<sizeof(true)<<endl; // 1
    cout<<"sizeof false: "<<sizeof(false)<<endl; // 1
    return 0;
}
cs



* 자료형 bool

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
#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;
}
cs



Posted by 너래쟁이
: