# Programming In C: Relational and Logical Operators

This article is part of a series – How to Program Anything: C Programming

### Preface

This article is a continuation on expressions that can be constructed in the C language. We’ve studied literal expressions in the previous article. This article will focus on expressions involving operators. If you remember, expressions are pieces of code that are executed right away to yield some kind of result, as opposed to a statement which may affect an entire program’s structure. An example of an expression could be the less than operator (<). When you compare two things to each other you result in either a true or false condition, is one less than the other? That true/false value becomes the result of the expression. Another example of an expression would be the logical and operator (&&), which tests two truth values to each other to come up with true/false result of its own: if the two values are both true than it is true as well.

Operators in C are half of the equation towards getting stuff done in your programs. The other half are the statements that make up the structure of a program, however, we have yet to get to that. Operators are basically actions that are taken in a program on the given data. It’s all well and good to define variables, which we’ve been doing so far, but we have to be able to *do* something with those variables.

Operators can be split up into five main classes: program, arithmetic, bitwise, logical, and relational. We covered arithmetic operators in the previous article in the series. In this article we focus on the relational and logical operators.

### Relational Operators

Relational operators are called such because they give us information about how values relate to each other. Is one value the same as another value, or is it less than or greater than? Relational operators give us true or false values. If one value is greater than the other, and that is what we test for, then the result of the operator is true, however if we are asking if something is greater and it indeed is less, than the result of the operator is false. This can be made more clear with a code excerpt:

1 2 3 |
5 < 7; // the result of this expression is true 5 > 7; // the result of this expression is false |

The results of these kind of operators are useful in what are known as if-clauses, which determine what happens depending on the result of a (group) of relational/logical operator(s). However, we haven’t covered if-clauses or statements yet, so you’ll have to take it on faith that the true and false results of these operators are useful. For something more concrete you can consider that in C true is anything other than zero, and false is zero. So, these expressions return 1 for true, and 0 for false.

NOTE: In C99 there is a _Bool data type defined. This data type could easily hold the 0 and 1 values returned by the relational and logical operators covered here.

There are six different relational operators as listed below:

- > Greater Than
- Is the left operand greater than the right operand?
- >= Greater Than or Equal
- Is the left operand greater than or equal to the right operand?
- < Less Than
- Is the left operand less than the right operand?
- <= Less Than or Equal
- Is the left operand less than or equal to the right operand?
- == Equal
- Is the left operand equal to the right operand?
- != Not Equal
- Is the left operand NOT equal to the right operand?

You would use the operators like so in the following code:

1 2 3 4 5 6 7 8 9 10 11 12 |
int a, b; a = 5; b = 8; a > 2; // true a < 2; // false a >= 5; // true b < 10; // true b >= 8; // true |

### Logical Operators

Logical operators follow the work of George Boole and his boolean logic. I’ve written previously about George Boole and this boolean operations in my post Algebraic Tron. In essence, you can perform a couple *binary* (two operands) operations on simple true and false values in C, and one *unary* (one operand) operand on the same. Logical operators take true and false values as their operands and depending on the operation return their own resultant true or false value.

There are two *binary* logical operators available to us ready-made in C, and these are the AND and OR operators. In the previous “tron” article I identified many more boolean operations that are available, but in C there is just AND and OR.

#### The And Operator

The AND operator is written in code as two ampersands: &&. It is structured in code to look like the following:

1 |
true/false value && true/false value |

The operation of && is pretty simple: if the left and right operands are both true, then the result of the operation is true. If either one, or both, of those operands are false then the result of the operation will be false. In my other article I set up what are called “truth tables.” A truth table of the AND operation may be more descriptive than the former sentence:

#### The Or Operator

The OR operator is written in code as two vertical bars: ||. It is structured in code to look like the following:

1 |
true/false value || true/false value |

The operation of || is pretty simple: if either the left *or* right operands in the expresson are true than the result of the expression is true; meaning that the expression is false only if both of the operands are false. In my other article I set up what are called “truth tables.” A truth table of the OR operation may be more descriptive than the former sentence:

#### The Not Operator

The NOT operator is the one *unary* operator (meaning it has one operand) that the C programming language offers. The not operator is simply the negation of whatever it is acting on. A true value becomes a false value, and a false false value is evaluated as a true value. The not operator is written in code as a single exclamation point: !. It is structured in code to look like the following:

1 |
!truefalse_value |

As written true becomes false and false becomes true. In my other article I set up what are called “truth tables.” A truth table of the NOT operation may be more descriptive than the former sentence:

#### Order of Precedence

It is important to note that all the relational and logical operators are of lower precedence than the arithmetic operators. This means that any kind of arithmetic that must be done in a composite expression is done first before any type of comparison is made. However, amongs themselves the relational and logical operators have their own order of precedence that looks like this (first is done first):

- ! (Not Operator)
- >, >=, < and <=
- == and !=
- && (And Operator)
- || (Or Operator)

Just as we previously did in the arithmetic operators article, you can use parentheses to alter the order of operations as well. Anything inside parentheses is evaluated first, from the the innermost parentheses outwards. We can examine what kind of effects this could have in a code example:

1 2 3 |
0 && 0 || 1 // evaluates to true or 1 0 && (0 || 1) // evaluates to false or 0 |

### Missing Operators

At this point an astute reader may be asking, “What about the other operators?” C doesn’t have a builtin operator for example for the XOR operation. However, this is because operators like XOR and such can be easily constructed from the basic operations avaiable, as shown in the aforementioned article on boolean logic. Following the example, if we wanted to construct a XOR expression, one where it only evaluates to true if only one of the operands is true (but not both), we’d write a composite expression like the following. This is also a good example of all the logical operators working together:

1 |
(a || b) && !(a && b) |

### Conclusion

Being able to compare the values of variables is extremely important in any C program. Later on we’ll cover how we can alter the flow of a program depending on the results of relational and logical operators, in essence creating programs or processes that can somewhat “think” for themselves. C provides an AND, OR, and NOT operators, but from these operators we can easily build any kind of more complex boolean logic operation, as we have done above with exclusive or (XOR). I hope I have provided insight into the ways C relational operators and logial operators work, thanks for reading!

This article is part of a series – How to Program Anything: C Programming

If you appreciate this article you might consider supporting my Patreon.

But if a monthly commitment is a bit much, I get it, you might consider buying me a coffee.

photo credit: nathaniel s South Bend Composition (with numbers) via photopin (license)

## 1 Response

[…] in a previous article in the series. We also already covered relational and logical operators in this article. If you’re looking for bitwise operators you can check out this article. In this article […]