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

May be interested

  • Stardust's effective way in Pokemon GOStardust's effective way in Pokemon GO
    pokemon go stardust is used to evolve pokemom with candy. therefore, if there is more stardust, it will make the evolution easier.
  • AZ word about eggs in Pokemon GoAZ word about eggs in Pokemon Go
    there are many different pokemon eggs that can hatch into many pokemon, but to make these eggs hatch into pokemon, you will have to perform some tasks. each player begins with an egg incubator, which allows the user to incubate an egg after performing certain steps, gps and pedometer tracking on the mobile device of you and the game.
  • Things you didn't know about 26 types of Pokéball - Part 1Things you didn't know about 26 types of Pokéball - Part 1
    pokéball is the most important item in pokémon go, allowing gamers to quickly catch pokémon back to their team. the higher the pokéball, the more effective the ability to catch pokémon.
  • 10 tips to help you become a great Pokemon trainer10 tips to help you become a great Pokemon trainer
    in the article below, tipsmake.com will introduce to you some tips to easily become a great trainer in this pokemon go game offline ...
  • How to use GoChat application in Pokémon GOHow to use GoChat application in Pokémon GO
    pokémon go has become a very hot phenomenon since its debut. any information or tricks related to the game are read and applied by players during the process of catching pokémon. and the gochat chat app for pokémon go players brings space to capture pokémon much more interesting.
  • How to play Pokemon GO in Landscape Mode on the iPhoneHow to play Pokemon GO in Landscape Mode on the iPhone
    although players can play pokemon go in portrait mode. however, if you want to watch and play games on a large and eye-catching screen, players can switch to playing games in landscape mode.
  • The secret to controlling Pokemon Go employees at workThe secret to controlling Pokemon Go employees at work
    these days, hr managers are faced with an extremely painful problem that is the status of priority employees playing pokemon go more than work. this has caused a small impact on productivity and efficiency.
  • Check out the 'buffalo' Pokémon in Pokémon GoCheck out the 'buffalo' Pokémon in Pokémon Go
    each type of pokemon has hp, cp, ability to attack and endure differently. based on these indicators, players can determine as well as choosing the most powerful pokemon for their offensive tactics.
  • Sitting home can also locate Pokemon around, do you believe it?Sitting home can also locate Pokemon around, do you believe it?
    the tightening of the niantic developers' rules to prevent players from abusing the support tools also brings annoyance, such as those who have no conditions to move much, go away, it is hard to know. get the location of the pokemon around the area they live in
  • 5 undeniable benefits when playing Pokemon Go5 undeniable benefits when playing Pokemon Go
    get to know many new people, breathe fresh air, relieve stress, increase concentration thanks to going out for a walk .... are compelling reasons to force you to try pokemon go now .