Boolean in C

Boolean, also known as bool in C, is an element that you must know when using this programming language. Here's everything you need to know about Boolean in C.

Boolean in C Picture 1

What is Boolean in C?

Usually, in programming, you will need a data type that can only have one of two values, such as:

  1. Yes/No - Yes/No
  2. On/Off - Turn on/Off
  3. True/False - True/False

For this matter, C has a bool data type also known as booleans. Booleans represent the values ​​true or false.

Boolean variables in C

In C, bool is not a built-in data type, like int or char. It was introduced in C99, and you must import the following header file to use it:

#include 

A boolean variable is declared with the keyword booland can only accept values true​​of either false:

bool isProgrammingFun = true; bool isFishTasty = false;

Before trying to print boolean variables, you need to know that boolean values ​​are returned as integers:

  1. 1(or any other number but not 0) representstrue
  2. 0representfalse

Therefore, you must use format specifiers %dto print a boolean value. For example:

#include #include // Nhập file header boolean int main() { bool isProgrammingFun = true; bool isFishTasty = false; printf("%dn", isProgrammingFun); // Kết quả 1 (true) printf("%d", isFishTasty); // Kết quả 0 (false) return 0; }

However, returning a boolean value by comparing values ​​and variables is more common.

Compare values ​​and variables

Comparing values ​​is useful in programming because it helps us find answers and make correct decisions.

For example, you can use comparison operators like greater than (>) to find the difference between two values:

#include int main() { printf("%d", 10 > 9); // Kết quả 1 (true) vì 10 lớn hơn 9 return 0; }

From the above example, you can see that the return value is a boolean value.

You can also compare two variables. For example:

#include int main() { int x = 10; int y = 9; printf("%d", x > y); // Kết quả 1 (true) vì 10 lớn hơn 9 return 0; }

In the example below, we use equal to ( ==) - the equality operator to compare different values:

#include int main() { printf("%dn", 10 == 10); // Kết quả 1 (true), vì 10 bằng 10 printf("%dn", 10 == 15); // Kết quả 0 (false), vì 10 nhỏ hơn 15 printf("%dn", 5.5 == 55); // Kết quả 0 (false) vì 5.5 không bằng 55 printf("%dn", 3.8 == 3.8); // Kết quả 1 (true) vì 3.8 bằng 3.8 return 0; }

C is not limited to comparing only numbers, you can also compare boolean variables, even special structures like arrays or arrays in C.

For example:

#include #include // Nhập file header boolean int main() { bool isHamburgerTasty = true; bool isPizzaTasty = true; printf("%d", isHamburgerTasty == isPizzaTasty); return 0; }

Remember to include the header file when working with variables bool.

Practical examples

Let's look at a real-life example where we need to know whether a person is of voting age or not.

The example below uses a comparison operator >=to see if age ( 25) is greater than or equal to the voting age limit set at 18:

#include int main() { int myAge = 25; int votingAge = 18; printf("%d", myAge >= votingAge); // Kết quả 1 (true), nghĩa là từ 25 tuổi trở lên được quyền tham gia bình chọn! return 0; }

Isn't it great? You can even wrap the 'code' above in a command if…else so we can perform different tasks depending on the result:

#include int main() { int myAge = 25; int votingAge = 18; if (myAge >= votingAge) { printf("Đủ tuổi bình chọn!"); } else { printf("Không đủ tuổi bỏ phiếu."); } return 0; }

Above are the things you need to know about boolean in C. Hope the article is useful to you.

4 ★ | 1 Vote