logo

C Boolean

В C Boolean е тип данни, който съдържа два типа стойности, т.е. 0 и 1. По принцип стойността на типа bool представлява два типа поведение, вярно или невярно. Тук '0' представлява фалшива стойност, докато '1' представлява истинска стойност.

В C Boolean, '0' се съхранява като 0, а друго цяло число се съхранява като 1. Ние не изискваме да използваме заглавен файл, за да използваме типа данни Boolean в C++ , но в C трябва да използваме заглавния файл, т.е. stdbool.h. Ако не използваме заглавния файл, тогава програмата няма да се компилира.

Синтаксис

 bool variable_name; 

В горния синтаксис, bool е типът данни на променливата и име_на_променлива е името на променливата.

Нека разберем чрез пример.

 #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. Булевият масив може да съдържа истинска или невярна стойност, а стойностите на масива могат да бъдат достъпни с помощта на индексиране.

Нека разберем този сценарий чрез пример.

 #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

Има друг начин за използване на булева стойност, т.е. typedef . По принцип 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; } 

В горния код използваме булевите стойности, т.е. true и false, но не сме използвали типа bool. Използваме булевите стойности, като създаваме ново име от типа 'bool'. За да се постигне това, typedef в програмата се използва ключова дума.

 typedef enum{false,true} b; 

Горният израз създава ново име за „ bool ' тип, т.е. 'b' като 'b' може да съдържа стойност true или false. Ние използваме тип 'b' в нашата програма и създаваме променливата 'x' от тип 'b'.

Изход

 The value of x is false 

Булеви с логически оператори

Булевата стойност на типа е свързана с логически оператори. Има три вида логически оператори в C език :

&&(И оператор): Това е логически оператор, който приема два операнда. Ако стойността на двата операнда е истина, тогава този оператор връща истина, в противен случай невярно

||(ИЛИ оператор): Това е логически оператор, който приема два операнда. Ако стойността на двата операнда е 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