logo

C Booleano

In C, Boolean è un tipo di dati che contiene due tipi di valori, ovvero 0 e 1. Fondamentalmente, il valore di tipo bool rappresenta due tipi di comportamento, vero o falso. Qui, '0' rappresenta il valore falso, mentre '1' rappresenta il valore vero.

In C Boolean, '0' viene memorizzato come 0 e un altro numero intero viene memorizzato come 1. Non è necessario utilizzare alcun file di intestazione per utilizzare il tipo di dati booleano in C++ , ma in C dobbiamo usare il file di intestazione, ovvero stdbool.h. Se non utilizziamo il file header, il programma non verrà compilato.

Sintassi

 bool variable_name; 

Nella sintassi precedente, bool è il tipo di dati della variabile e nome_variabile è il nome della variabile.

Capiamo attraverso un esempio.

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

Nel codice precedente, abbiamo utilizzato file header in modo da poter utilizzare la variabile di tipo bool nel nostro programma. Dopo la dichiarazione del file header creiamo la variabile di tipo bool ' X ' e assegna un ' falso ' valore ad esso. Quindi aggiungiamo le istruzioni condizionali, ovvero se altro , per determinare se il valore di 'x' è vero o no.

Produzione

 The value of x is FALSE 

Matrice booleana

Ora creiamo un array di tipo bool. L'array booleano può contenere un valore vero o falso ed è possibile accedere ai valori dell'array con l'aiuto dell'indicizzazione.

Cerchiamo di comprendere questo scenario attraverso un esempio.

 #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

Esiste un altro modo di utilizzare il valore booleano, ovvero typedef . Fondamentalmente typedef è una parola chiave in linguaggio C, che viene utilizzata per assegnare il nome al tipo di dati già esistente.

Vediamo un semplice esempio di 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; } 

Nel codice precedente utilizziamo i valori booleani, ovvero true e false, ma non abbiamo utilizzato il tipo bool. Utilizziamo i valori booleani creando un nuovo nome del tipo 'bool'. Per raggiungere questo obiettivo, il typedef la parola chiave viene utilizzata nel programma.

 typedef enum{false,true} b; 

L'istruzione precedente crea un nuovo nome per ' bool ', ovvero 'b' poiché 'b' può contenere un valore vero o falso. Usiamo il tipo 'b' nel nostro programma e creiamo la variabile 'x' di tipo 'b'.

Produzione

 The value of x is false 

Booleano con operatori logici

Il valore di tipo booleano è associato agli operatori logici. Esistono tre tipi di operatori logici nel file Linguaggio C :

&&(Operatore AND): È un operatore logico che accetta due operandi. Se il valore di entrambi gli operandi è vero, allora questo operatore restituisce vero altrimenti falso

||(Operatore OR): È un operatore logico che accetta due operandi. Se il valore di entrambi gli operandi è falso, restituisce falso altrimenti vero.

!(NON Operatore): È un operatore NOT che accetta un operando. Se il valore dell'operando è falso, restituisce vero, mentre se il valore dell'operando è vero, restituisce falso.

Capiamo attraverso un esempio.

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

Produzione

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