Home >Backend Development >C#.Net Tutorial >How to use bool in c language
The bool type in C language represents true/false, and the value is 1 (true) or 0 (false). You can use bool is_true = true; to declare and initialize Boolean variables, or you can use the true/false keyword. Bool variables can use logical NOT, AND, OR, and XOR operations. Bool expressions are used in conditional statements and loops. The bool type can be implicitly converted to the int type (1: true, 0: false); the int type can also be implicitly converted to the bool type (non-zero: true, 0: false).
Usage of bool
type in C language
In C language, ## The #bool type is a Boolean type, used to represent true (true) or false (false). It is a basic data type whose value can only be
1 (true) or
0 (false).
Declare a bool variable
bool variable, you can use the following syntax:
<code class="c">bool is_true;</code>
InitializationboolVariables
boolVariables:
<code class="c">bool is_true = true; bool is_false = 0;</code>Alternatively, you can Using the
true and
false keywords:
<code class="c">bool is_true = true; bool is_false = false;</code>
boolOperation
bool variable:
):
changes true to false and vice versa.
):
The result is true only when both operands are true.
):
When any one operand is true, the result is true.
):
The result is true only when the two operands are not the same.
boolexpression
boolexpression evaluates to
true Or an expression of
false. They are commonly used in conditional statements and loops.
<code class="c">if (is_true) { // 真值代码块 } else { // 假值代码块 }</code>
bool and integer conversion
type can be implicitly converted For int
type:
true
, it is converted to 1
.
When the false
, it is converted to 0
.
values can be implicitly converted to bool
types:
.
When the
int.
The above is the detailed content of How to use bool in c language. For more information, please follow other related articles on the PHP Chinese website!