logo

C booleà

En C, Boolean és un tipus de dades que conté dos tipus de valors, és a dir, 0 i 1. Bàsicament, el valor del tipus bool representa dos tipus de comportament, ja sigui cert o fals. Aquí, '0' representa un valor fals, mentre que '1' representa un valor veritable.

En C booleà, '0' s'emmagatzema com a 0 i un altre enter s'emmagatzema com a 1. No necessitem utilitzar cap fitxer de capçalera per utilitzar el tipus de dades booleà a C++ , però en C, hem d'utilitzar el fitxer de capçalera, és a dir, stdbool.h. Si no fem servir el fitxer de capçalera, el programa no es compilarà.

Sintaxi

 bool variable_name; 

En la sintaxi anterior, bool és el tipus de dades de la variable i nom_variable és el nom de la variable.

Entenem-ho a través d'un exemple.

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

En el codi anterior, hem utilitzat fitxer de capçalera perquè puguem utilitzar la variable de tipus bool al nostre programa. Després de la declaració del fitxer de capçalera, creem la variable de tipus bool ' x ' i assigna un ' fals 'valor per a això. Aleshores, afegim les declaracions condicionals, és a dir, si una altra cosa , per determinar si el valor de 'x' és cert o no.

Sortida

 The value of x is FALSE 

Matriu booleà

Ara, creem una matriu de tipus bool. La matriu booleana pot contenir un valor vertader o fals, i es pot accedir als valors de la matriu amb l'ajuda de la indexació.

Entenem aquest escenari a través d'un exemple.

 #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

Hi ha una altra manera d'utilitzar el valor booleà, és a dir, typedef . Bàsicament, typedef és una paraula clau en llenguatge C, que s'utilitza per assignar el nom al tipus de dades ja existent.

Vegem un exemple senzill de 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; } 

En el codi anterior, utilitzem els valors booleans, és a dir, true i false, però no hem utilitzat el tipus bool. Utilitzem els valors booleans creant un nou nom del tipus 'bool'. Per aconseguir-ho, el typedef la paraula clau s'utilitza al programa.

 typedef enum{false,true} b; 

La declaració anterior crea un nom nou per a ' bool ', és a dir, 'b' com a 'b' pot contenir un valor vertader o fals. Utilitzem el tipus 'b' al nostre programa i creem la variable 'x' del tipus 'b'.

Sortida

 The value of x is false 

Booleà amb operadors lògics

El valor de tipus booleà està associat amb operadors lògics. Hi ha tres tipus d'operadors lògics al llenguatge C :

&&(AND Operador): És un operador lògic que pren dos operands. Si el valor dels dos operands és cert, aquest operador retorna vertader, en cas contrari, fals

||(Operador OR): És un operador lògic que pren dos operands. Si el valor dels dos operands és fals, retorna false, en cas contrari, és cert.

!(NO Operador): És un operador NOT que pren un operand. Si el valor de l'operand és fals, retorna vertader, i si el valor de l'operand és cert, retorna fals.

Entenem-ho a través d'un exemple.

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

Sortida

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