42 Piscine/42 Piscine: Preparation Day 1/Variables and Data Types/bool

In C, boolean values are typically represented using the `\_Bool` data type or, more commonly, using a header from the C99 standard and later. The `stdbool.h` header provides a standard way to work with boolean values by defining the `bool` data type and two constants: `true` and `false`. Here's how boolean values work in C:

### `stdbool.h` Header

To use boolean values, include the `stdbool.h` header at the beginning of your C program:

```
#include <stdbool.h>

```

### `bool` Data Type

After including `stdbool.h`, you can declare boolean variables using the `bool` data type:

```
bool isTrue = true;
bool isFalse = false;

```

Here, `isTrue` and `isFalse` are boolean variables that can store either `true` or `false.

### Constants

The `stdbool.h` header defines two constants, `true` and `false`, which are used to represent boolean values. They are typically used in conditional statements:

```
bool isSunny = true;
bool isRaining = false;

if (isSunny) {
    printf("It's a sunny day!\n");
}

if (!isRaining) {
    printf("It's not raining.\n");
}

```

In C, 0 represents `false`, and any non-zero value represents `true`. The constants `true` and `false` are defined to help make your code more readable and maintainable.

### Logical Operators

You can use logical operators like `&&` (logical AND), `||` (logical OR), and `!` (logical NOT) to perform boolean operations:

```
bool hasMoney = true;
bool hasCreditCard = true;

if (hasMoney && hasCreditCard) {
    printf("You can make a purchase!\n");
}

```

### Comparison Operators

Comparison operators like `==` (equal), `!=` (not equal), `<` (less than), `>` (greater than), `<=` (less than or equal), and `>=` (greater than or equal) are often used to compare values and obtain boolean results:

```
int x = 5;
int y = 3;

bool isEqual = (x == y);  // false
bool isGreater = (x > y); // true

```

```
int x = 5;
int y = 3;

bool isEqual = (x == y);  // false
bool isGreater = (x > y); // true

```

1. `int x = 5;` and `int y = 3;`: These lines declare two integer variables `x` and `y` and assign them values. `x` is assigned the value 5, and `y` is assigned the value 3.

2. `bool isEqual = (x == y);`: This line is using the `==` operator, which is the equality operator. It checks if the value of `x` is equal to the value of `y`. In this case, `x` is 5, and `y` is 3. Since 5 is not equal to 3, the expression `(x == y)` evaluates to `false`, and `isEqual` is assigned the value `false`.

3. `bool isGreater = (x > y);`: This line uses the `>` operator, which is the greater-than operator. It checks if the value of `x` is greater than the value of `y`. In this case, `x` is 5, and `y` is 3. Since 5 is indeed greater than 3, the expression `(x > y)` evaluates to `true`, and `isGreater` is assigned the value `true`.

**Further breakdown**

```
int x = 5;
int y = 3;

bool isEqual = (x == y);  // false
bool isGreater = (x > y); // true

```

**1. `int x = 5;` and `int y = 3;`** These lines declare two integer variables `x` and `y` and assign them values. `x` is assigned the value 5, and `y` is assigned the value 3.
**2. `bool isEqual = (x == y);`** This line is using the `==` operator, which is the equality operator. It checks if the value of `x` is equal to the value of `y`. In this case, `x` is 5, and `y` is 3. Since 5 is not equal to 3, the expression `(x == y)` evaluates to `false`, and `isEqual` is assigned the value `false`.
**3. `bool isGreater = (x > y);`** This line uses the `>` operator, which is the greater-than operator. It checks if the value of `x` is greater than the value of `y`. In this case, `x` is 5, and `y` is 3. Since 5 is indeed greater than 3, the expression `(x > y)` evaluates to `true`, and `isGreater` is assigned the value `true`.

**To summarize:**

* `isEqual` is `false` because `x` is not equal to `y` (5 is not equal to 3).
* `isGreater` is `true` because `x` is greater than `y` (5 is greater than 3).

Comparison operators are commonly used to make decisions in your code by comparing values and determining whether specific conditions are met or not.

### Conditional Statements

Boolean values are commonly used in conditional statements (e.g., `if`, `else if`, and `else`) to control the flow of your program based on certain conditions:

```
int age = 20;
bool isAdult = (age >= 18);

if (isAdult) {
    printf("You are an adult.\n");
} else {
    printf("You are not an adult.\n");
}

```

In C, boolean values are a fundamental part of decision-making and control flow in your programs. The use of the `stdbool.h` header and the `bool` data type helps improve code clarity and readability, making it easier to work with true/false conditions.