Boolean Operators โ๏ธ#
In programming the values true or false are called boolean values. In C, there is no dedicated data type for booleans. Instead, true is represented by any non-zero integer and false is represented by 0.
Therefore boolean operators, or logic operators, are operators that perform logical comparisons that evaluate to either 1 (true) or 0 (false).
C contains all the comparisons you would find in regular mathematics. Hereโs a list of all of the operators:
Name |
Symbol |
Description |
Example |
---|---|---|---|
Not |
|
Is placed infront of an expression. Inverts the boolean value of the experssion, converting any non-zero numbers to zero and converting zero to one. |
|
Equals |
|
Checks if two values of the same type equal each other |
|
Not equals |
|
Checks if two values of the same type are not equal to each other |
|
Greater than |
|
Checks if a number is greater than another number |
|
Greater or equal to |
|
Checks if a number is greater than or equal to another number |
|
Less than |
|
Checks if a number is less than another number |
|
Less than or equal to |
|
Checks if a number is less than or equal to another number |
|
Or |
|
Checks if either of two boolean values is true. |
``1 || 1 is true, 0 || 1 is true, 0 || 0 is false `` |
And |
|
Checks if both of two boolean values is true. |
|
Ex.
int boolean_result = 1 == 2;
// boolean_result is 0 (false)
boolean_result = 1 != 2;
// boolean_result is 1 (true)
boolean_result = 3 >= 3;
// boolean_result is 1 (true)
boolean_result = 0; // (false)
boolean_result = !boolean_result;
// boolean_result is 1 (true)
Boolean operators have lower precedence than math operators, meaning math operators will be evaluated first. But when in doubt, use parenthesis to explictly group your expressions!
Fun fact
Booleans are named after George Boole, who first defined an algebraic system of logic in the mid 19th century
Tasks ๐ฏ#
Write a program to print the result of the statement โ50 plus 25 is greater than 40โ:
Solution โ
Should print out
> 1
#include <stdio.h> int main() { printf("%d", 50 + 25 > 40); return 0; }