logo

C Boolescher Wert

In C ist Boolean ein Datentyp, der zwei Arten von Werten enthält, nämlich 0 und 1. Grundsätzlich repräsentiert der Wert vom Typ Bool zwei Arten von Verhalten, entweder wahr oder falsch. Hier steht „0“ für einen falschen Wert, während „1“ für einen wahren Wert steht.

In C Boolean wird „0“ als 0 und eine andere ganze Zahl als 1 gespeichert. Wir benötigen keine Header-Datei, um den Datentyp „Boolean“ zu verwenden C++ , aber in C müssen wir die Header-Datei verwenden, d. h. stdbool.h. Wenn wir die Header-Datei nicht verwenden, wird das Programm nicht kompiliert.

Syntax

 bool variable_name; 

In der obigen Syntax gilt: bool ist der Datentyp der Variablen und Variablennamen ist der Name der Variablen.

Lassen Sie es uns anhand eines Beispiels verstehen.

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

Im obigen Code haben wir verwendet Header-Datei, damit wir die Variable vom Typ Bool in unserem Programm verwenden können. Nach der Deklaration der Header-Datei erstellen wir die Variable vom Typ Bool ' X ' und weist ein ' zu FALSCH ' Wert darauf. Dann fügen wir die bedingten Anweisungen hinzu, d. h. ansonsten , um zu bestimmen, ob der Wert von „x“ wahr ist oder nicht.

Ausgabe

 The value of x is FALSE 

Boolesches Array

Jetzt erstellen wir ein Array vom Typ Bool. Das boolesche Array kann entweder einen wahren oder einen falschen Wert enthalten, und auf die Werte des Arrays kann mit Hilfe der Indizierung zugegriffen werden.

Lassen Sie uns dieses Szenario anhand eines Beispiels verstehen.

 #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++)>

Typdefinition

Es gibt eine andere Möglichkeit, boolesche Werte zu verwenden, nämlich Typdefinition . Grundsätzlich ist typedef ein Schlüsselwort in der C-Sprache, mit dem der Name dem bereits vorhandenen Datentyp zugewiesen wird.

Sehen wir uns ein einfaches Beispiel für typedef an.

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

Im obigen Code verwenden wir die booleschen Werte, also true und false, aber wir haben nicht den Typ bool verwendet. Wir verwenden die booleschen Werte, indem wir einen neuen Namen vom Typ „bool“ erstellen. Um dies zu erreichen, die Typdefinition Das Schlüsselwort wird im Programm verwendet.

 typedef enum{false,true} b; 

Die obige Anweisung erstellt einen neuen Namen für das ' bool '-Typ, d. h. 'b', da 'b' entweder einen wahren oder einen falschen Wert enthalten kann. Wir verwenden in unserem Programm den Typ „b“ und erstellen die Variable „x“ vom Typ „b“.

Ausgabe

 The value of x is false 

Boolescher Wert mit logischen Operatoren

Der Wert vom Typ Boolean ist mit logischen Operatoren verknüpft. Es gibt drei Arten von logischen Operatoren C Sprache :

&&(AND-Operator): Es handelt sich um einen logischen Operator, der zwei Operanden benötigt. Wenn der Wert beider Operanden wahr ist, gibt dieser Operator „wahr“ zurück, andernfalls „falsch“.

||(OR-Operator): Es handelt sich um einen logischen Operator, der zwei Operanden benötigt. Wenn der Wert beider Operanden falsch ist, wird „false“ zurückgegeben, andernfalls „true“.

!(NICHT Operator): Es handelt sich um einen NOT-Operator, der einen Operanden benötigt. Wenn der Wert des Operanden falsch ist, wird true zurückgegeben, und wenn der Wert des Operanden wahr ist, wird false zurückgegeben.

Lassen Sie es uns anhand eines Beispiels verstehen.

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

Ausgabe

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