logo

C Boolean

V C je Boolean podatkovni tip, ki vsebuje dve vrsti vrednosti, tj. 0 in 1. V bistvu vrednost tipa bool predstavlja dve vrsti vedenja, bodisi resnično ali napačno. Tukaj '0' predstavlja lažno vrednost, medtem ko '1' predstavlja pravo vrednost.

V C Boolean je '0' shranjen kot 0, drugo celo število pa je shranjeno kot 1. Za uporabo podatkovnega tipa Boolean v C++ , toda v C-ju moramo uporabiti datoteko glave, tj. stdbool.h. Če ne uporabimo datoteke glave, se program ne bo prevedel.

Sintaksa

 bool variable_name; 

V zgornji sintaksi je bool je podatkovni tip spremenljivke in ime_spremenljivke je ime spremenljivke.

Razumejmo skozi primer.

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

V zgornji kodi smo uporabili datoteko glave, tako da lahko v našem programu uporabimo spremenljivko tipa bool. Po deklaraciji datoteke glave ustvarimo spremenljivko tipa bool ' x 'in dodeli' lažno ' vrednost za to. Nato dodamo pogojne stavke, tj. če potem , da ugotovi, ali je vrednost 'x' resnična ali ne.

Izhod

 The value of x is FALSE 

Logična matrika

Zdaj ustvarimo matriko tipa bool. Logična matrika lahko vsebuje vrednost true ali false, do vrednosti matrike pa je mogoče dostopati s pomočjo indeksiranja.

Razumejmo ta scenarij na primeru.

 #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

Obstaja še en način uporabe logične vrednosti, tj. typedef . V bistvu je typedef ključna beseda v jeziku C, ki se uporablja za dodelitev imena že obstoječemu podatkovnemu tipu.

Oglejmo si preprost primer 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; } 

V zgornji kodi uporabljamo logične vrednosti, tj. true in false, vendar nismo uporabili tipa bool. Logične vrednosti uporabimo tako, da ustvarimo novo ime tipa 'bool'. Da bi to dosegli, tipdef ključna beseda se uporablja v programu.

 typedef enum{false,true} b; 

Zgornji stavek ustvari novo ime za ' bool ', tj. 'b' kot 'b' lahko vsebuje vrednost true ali false. V našem programu uporabljamo tip 'b' in ustvarimo spremenljivko 'x' tipa 'b'.

Izhod

 The value of x is false 

Logično z logičnimi operatorji

Vrednost tipa Boolean je povezana z logičnimi operatorji. Obstajajo tri vrste logičnih operatorjev v jezik C :

&&(operator IN): Je logični operator, ki ima dva operanda. Če sta vrednosti obeh operandov true, potem ta operator vrne true, sicer false

||(operator ALI): Je logični operator, ki ima dva operanda. Če je vrednost obeh operandov false, potem vrne false, sicer pa true.

!(NI operater): To je operator NOT, ki sprejme en operand. Če je vrednost operanda false, potem vrne true, in če je vrednost operanda true, potem vrne false.

Razumejmo skozi primer.

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

Izhod

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