Operators
Tags: c
Published: 2022-11-29 11:11:10

More challenges given by ALX, more stuff to read about and grasp. Honestly, its overwhelming. Sure, I do have some basic knowledge with JavaScript, should be easy right?
Yeah, it should be, if the challenges did not have conditions I gotta follow. I mean, why do I need to use putchars, puts when I can use printf and call it a day right? Boy, was I Wrong. Enough rant, let's get to documenting what I've learnt.
Reminder: Mostly me documenting stuff, not meant to be an alternative to what you learn (fact-checking maybe) and also, I am talking about C, not JavaScript (even if their syntax seems familiar).

## Operators
Operators are symbols that allows a program to perform specific and logical tasks/operations.
There are several types of operators in C:

- Arithmetic operators
- Relational operators
- Logical/boolean operators

## Arithmetic operators
Arithmetic operators are mathematical expressions. There are seven of em:

- Plus (+)
-  Minus (-)
- Multiplication (*)
- Division (/)
- Modulus (%)
-  Increment (++)
- Decrement (--)

Arithmetic operators are of two types. Unary and binary operators.
Unary operators are operators that works with an operand. Using the increment operator is a perfect example: `foo++`

Binary operators on the other hand, works with two or more operands. We use em everyday and everywhere, from calculating the speeds to safely land a spacecraft at NASA to calculating how much tip to give the pizza guy (1 + 1 anyone?)

### Relational Operators
Relational operators according to wikipedia, it is a construct that defines some kind of relationship between two entities. Methinks they are right, but swap out entities with operands. Here's a list of em;
```
> greater than
< less than
>= greater than or equal to
<= less than or equal to
== equal to
!= not equal to
```
Examples below;
```
foo == foo
bar > baz
```

### Logical/Boolean Operators
Logical operators are operators used to combine two or more relational operators. They are used with statements (more on that).
Below is a list of them;
- Logical AND (&&)
- Logical OR (||)
- Logical NOT (!)

There are more types of logical operators, like the XOR, AND and OR operators but for my sanity, lets talk about the ones that I end up seeing almost everyday.

### Use of The Listed Operators
- AND (&&) checks if both conditions in a statement is true. If the first one is false, the entire statement is false, irrespective of if the second condition is true. Example;
```
foo == foo && baz == baz        /*true*/
foo == bar && baz == baz        /*false*/
```
- OR(||) checks if either conditions given is true, meaning if one of them is false, the entire statement is evaluated as true unless both are false. Example;
```
foo == bar || baz == baz        /*true*/
foo == bar || bar == baz        /*false*/
```
- NOT(!) checks the conditions passed and returns the opposite value of the argument.
Take these snippets;
```
!(1>2)
!(1<2)
```
The first snippet says that 1 is greater than 2, which is **false**. But, there's the NOT operator, which will inverse the value of the argument and that will make it **true** because 1 is **NOT** greater than 2.
Going by the same logic for the second snippet, 1 is not less than 2. The result will be false because it is not true.

There are other types of operators, like assignment operators (+=, -=,...), tenary operators, sizeof operators...
Thing is, every language got an operator(s) that's specific/works well for them. Depending on what language syntax you choose to learn, don't mix up their operators. In other words, RTFM or look up some other source if the docs looks like someone tried transferring his/her frustrations onto its readers.

For book recommendation, check out K.N. King's C Programming: A Modern Approach (make sure its the second edition you got). Should be worth the read for both beginners and anyone looking for a refresher.
If you are done with that, pick up K & R ANSI C book. I have heard people say one should start with this book but nah, rather have most of it explained instead of doing a "shoot first, ask questions later" approach (debugging on the first chapter was NOT fun, especially if the issue was obvious if I knew what I was doing).

Ah well, back to real life. New episode of Chainsaw Man out. Need a bit of fun before doing those mock interviews (who in their right mind thought it was a good idea to have several rounds of interviews for entry level roles???)