logo

C 부울

C에서 Boolean은 0과 1의 두 가지 유형의 값을 포함하는 데이터 유형입니다. 기본적으로 bool 유형 값은 true 또는 false의 두 가지 유형의 동작을 나타냅니다. 여기서 '0'은 거짓값을 나타내고, '1'은 참값을 나타낸다.

C Boolean에서는 '0'이 0으로 저장되고, 다른 정수는 1로 저장됩니다. Boolean 데이터 유형을 사용하기 위해 헤더 파일을 사용할 필요는 없습니다. C++ , 그러나 C에서는 헤더 파일, 즉 stdbool.h를 사용해야 합니다. 헤더 파일을 사용하지 않으면 프로그램이 컴파일되지 않습니다.

통사론

 bool variable_name; 

위 구문에서, 부울 변수의 데이터 유형입니다. 변수_이름 변수의 이름입니다.

예를 통해 이해해 봅시다.

 #include #include int main() { bool x=false; // variable initialization. if(x==true) // conditional statements { printf('The value of x is true'); } else printf('The value of x is FALSE'); return 0; } 

위의 코드에서 우리는 프로그램에서 bool 유형 변수를 사용할 수 있도록 헤더 파일을 만듭니다. 헤더 파일 선언 후 bool 유형 변수 '를 생성합니다. 엑스 '를 할당하고 ' 거짓 '에 가치를 둡니다. 그런 다음 조건문을 추가합니다. 즉, 다른 경우라면 , 'x' 값이 참인지 아닌지 확인합니다.

산출

 The value of x is FALSE 

부울 배열

이제 bool 유형 배열을 만듭니다. 부울 배열에는 true 또는 false 값이 포함될 수 있으며, 인덱싱을 통해 배열 값에 액세스할 수 있습니다.

예를 통해 이 시나리오를 이해해 보겠습니다.

 #include #include int main() { bool b[2]={true,false}; // Boolean type array for(int i=0;i<2;i++) for loop { printf('%d,',b[i]); printf statement } return 0; < pre> <p>In the above code, we have declared a Boolean type array containing two values, i.e., true and false.</p> <p> <strong>Output</strong> </p> <pre> 1,0, </pre> <h2>typedef</h2> <p>There is another way of using Boolean value, i.e., <strong>typedef</strong> . Basically, typedef is a keyword in C language , which is used to assign the name to the already existing datatype.</p> <p> <strong>Let&apos;s see a simple example of typedef.</strong> </p> <pre> #include typedef enum{false,true} b; int main() { b x=false; // variable initialization if(x==true) // conditional statements { printf(&apos;The value of x is true&apos;); } else { printf(&apos;The value of x is false&apos;); } return 0; } </pre> <p>In the above code, we use the Boolean values, i.e., true and false, but we have not used the bool type. We use the Boolean values by creating a new name of the &apos;bool&apos; type. In order to achieve this, <strong>the typedef</strong> keyword is used in the program.</p> <pre> typedef enum{false,true} b; </pre> <p>The above statement creates a new name for the &apos; <strong>bool</strong> &apos; type, i.e., &apos;b&apos; as &apos;b&apos; can contain either true or false value. We use the &apos;b&apos; type in our program and create the &apos;x&apos; variable of type &apos;b&apos;.</p> <p> <strong>Output</strong> </p> <pre> The value of x is false </pre> <h2>Boolean with Logical Operators</h2> <p>The Boolean type value is associated with logical operators. There are three types of logical operators in the <a href="/c-programming-language-tutorial">C language</a> :</p> <p> <strong>&amp;&amp;(AND Operator):</strong> It is a logical operator that takes two operands. If the value of both the operands are true, then this operator returns true otherwise false</p> <p> <strong>||(OR Operator):</strong> It is a logical operator that takes two operands. If the value of both the operands is false, then it returns false otherwise true.</p> <p> <strong>!(NOT Operator):</strong> It is a NOT operator that takes one operand. If the value of the operand is false, then it returns true, and if the value of the operand is true, then it returns false.</p> <p> <strong>Let&apos;s understand through an example.</strong> </p> <pre> #include #include int main() y); printf(&apos;
The value of !x is %d&apos;, !x); </pre> <p> <strong>Output</strong> </p> <pre> The value of x&amp;&amp;y is 0 The value of x||y is 1 The value of !x is 1 </pre> <hr></2;i++)>

형식 정의

부울 값을 사용하는 또 다른 방법이 있습니다. 즉, 형식 정의 . 기본적으로 typedef는 이미 존재하는 데이터 유형에 이름을 할당하는 데 사용되는 C 언어의 키워드입니다.

typedef의 간단한 예를 살펴보겠습니다.

 #include typedef enum{false,true} b; int main() { b x=false; // variable initialization if(x==true) // conditional statements { printf(&apos;The value of x is true&apos;); } else { printf(&apos;The value of x is false&apos;); } return 0; } 

위 코드에서는 Boolean 값, 즉 true와 false를 사용했지만 bool 유형은 사용하지 않았습니다. 'bool' 유형의 새 이름을 만들어 부울 값을 사용합니다. 이를 달성하기 위해, 형식 정의 프로그램에서 키워드가 사용됩니다.

 typedef enum{false,true} b; 

위의 명령문은 '에 대한 새 이름을 만듭니다. 부울 ' 유형, 즉 'b'인 'b'는 true 또는 false 값을 포함할 수 있습니다. 우리 프로그램에서는 'b' 유형을 사용하고 'b' 유형의 'x' 변수를 생성합니다.

산출

 The value of x is false 

논리 연산자를 사용한 부울

부울 유형 값은 논리 연산자와 연관됩니다. 논리 연산자에는 세 가지 유형이 있습니다. C 언어 :

&&(AND 연산자): 두 개의 피연산자를 취하는 논리 연산자입니다. 두 피연산자의 값이 모두 true이면 이 연산자는 true를 반환하고 그렇지 않으면 false를 반환합니다.

||(OR 연산자): 두 개의 피연산자를 취하는 논리 연산자입니다. 두 피연산자의 값이 모두 false이면 false를 반환하고 그렇지 않으면 true를 반환합니다.

!(연산자 아님): 하나의 피연산자를 취하는 NOT 연산자입니다. 피연산자의 값이 false이면 true를 반환하고, 피연산자의 값이 true이면 false를 반환합니다.

예를 통해 이해해 봅시다.

 #include #include int main() y); printf(&apos;
The value of !x is %d&apos;, !x); 

산출

 The value of x&amp;&amp;y is 0 The value of x||y is 1 The value of !x is 1