logo

C booleano

En C, booleano es un tipo de datos que contiene dos tipos de valores, es decir, 0 y 1. Básicamente, el valor de tipo booleano representa dos tipos de comportamiento, verdadero o falso. Aquí, '0' representa un valor falso, mientras que '1' representa un valor verdadero.

En C Boolean, '0' se almacena como 0 y otro número entero se almacena como 1. No necesitamos usar ningún archivo de encabezado para usar el tipo de datos booleano en C++ , pero en C tenemos que usar el archivo de encabezado, es decir, stdbool.h. Si no utilizamos el archivo de encabezado, el programa no se compilará.

Sintaxis

 bool variable_name; 

En la sintaxis anterior, booleano es el tipo de datos de la variable, y nombre de la variable es el nombre de la variable.

Entendamos a través de un ejemplo.

 #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 código anterior, hemos utilizado archivo de encabezado para que podamos usar la variable de tipo bool en nuestro programa. Después de la declaración del archivo de encabezado, creamos la variable de tipo bool ' X ' y asigna un ' FALSO ' valor para ello. Luego, agregamos las declaraciones condicionales, es decir, si...si no , para determinar si el valor de 'x' es verdadero o no.

Producción

 The value of x is FALSE 

Matriz booleana

Ahora creamos una matriz de tipo bool. La matriz booleana puede contener valores verdaderos o falsos, y se puede acceder a los valores de la matriz con la ayuda de la indexación.

Entendamos este escenario a través de un ejemplo.

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

definición de tipo

Hay otra forma de utilizar el valor booleano, es decir, definición de tipo . Básicamente, typedef es una palabra clave en lenguaje C, que se utiliza para asignar el nombre al tipo de datos ya existente.

Veamos un ejemplo sencillo 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 código anterior, usamos valores booleanos, es decir, verdadero y falso, pero no usamos el tipo booleano. Usamos los valores booleanos creando un nuevo nombre del tipo 'bool'. Para lograr esto, el tipo de definición La palabra clave se utiliza en el programa.

 typedef enum{false,true} b; 

La declaración anterior crea un nuevo nombre para el ' booleano ' escriba, es decir, 'b' ya que 'b' puede contener un valor verdadero o falso. Usamos el tipo 'b' en nuestro programa y creamos la variable 'x' de tipo 'b'.

Producción

 The value of x is false 

Booleano con operadores lógicos

El valor de tipo booleano está asociado con operadores lógicos. Hay tres tipos de operadores lógicos en el lenguaje c :

&&(Operador Y): Es un operador lógico que toma dos operandos. Si el valor de ambos operandos es verdadero, entonces este operador devuelve verdadero, de lo contrario es falso.

||(Operador O): Es un operador lógico que toma dos operandos. Si el valor de ambos operandos es falso, devuelve falso; de lo contrario, es verdadero.

!(NO Operador): Es un operador NOT que toma un operando. Si el valor del operando es falso, devuelve verdadero, y si el valor del operando es verdadero, devuelve falso.

Entendamos a través de un ejemplo.

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

Producción

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