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.
What is Boolean in C?
Usually, in programming, you will need a data type that can only have one of two values, such as:
- Yes/No - Yes/No
- On/Off - Turn on/Off
- 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 bool
and 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
(or any other number but not0
) representstrue
0
representfalse
Therefore, you must use format specifiers %d
to 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.
You should read it
May be interested
- If ... Else in Cwhat is the if … else condition in c? what is the meaning and usage of if else statement in c? let's find out with tipsmake.com.com!
- How to Compile C Programs with GNU (GCC)tipsmake today will teach you how to compile a c program from source code using gnu (full name is gnu compiler collection, abbreviated gcc) - compiler for linux and minimalist gnu (mingw) on windows.
- How to Design and Implement a Field Using C Sharpthe first step in designing a class usually is to define its fields. fields are the backbone that classes rely upon, and so, the process of designing and implementing fields is crucial to the overall process of designing classes. this...
- How to Make Your Windows Forms Application Run at Windows Startup Using Csometimes you need to run your windows forms application when the user log in to windows. many applications that use this behavior are windows live messenger and yahoo messenger. in this article you will learn how to make your application...
- How to Set Up an OpenGL SDL GLEW Template Project in Visual Studiomany programmers prefer opengl for graphics. if you are one of them, you are strongly advised by its producer, to use a window toolkit (such as sdl) and an opengl loading libraries (such as glew). this guide will help you get over the...
- How to Use Function Template Parameter Packs in C++c++ template parameter packs (also known as variadic templates) were introduced in the c++11 standard and are powerful tools that let functions accept an arbitrary number of arguments. without further ado, jump into your favourite ide,...