C - Basic Operators
Tags: c
Published: 2020-12-23 04:45:52

# Table of Contents:
* [Overview](#overview)
* [Prefix, Infix, and Postfix](#binary-unary-prefix-infix-and-postfix)
* [Assignment](#assignment)
* [Arithmetic](#arithmetic)
* [Logical](#logical)
* [Relational](#relational)
* [Bitwise](#bitwise)

## Overview

What are operators? 

Operators are used to manipulate data. There are many different kinds of operators, such as assignment, arithmetic, logical, relational, and bitwise operators.

In C, the operators themselves are represented by symbols. For example, the addition operator would be the plus sign: `+`. But what do these symbols represent in a program? **Operators themselves are _functions_ that accept arguments**.

### Binary, Unary, Prefix, Infix, and Postfix

An operand is what an operator operates on.

> In mathematics an operand is the object of a mathematical operation

Operators will use one of the three 'fix' styles above, depending on the operator used.

Binary operators work with two operands. The addition `+` function grabs the left operand and the right operand, adding them together, then returning the result.

Unary operators work with one operand. The increment operator is a good example of this:

```c
int number = 10;

printf("This is the Postfix style: %d\n", number++);
// This is the Postfix style result: 10
printf("Now the variable number is assigned: %d\n", number);
// Now the variable number is assigned: 11

printf("This is the Prefix style result: %d\n", ++number);
// This is the Prefix style result: 12
printf("The variable number was already assigned: %d\n", number);
// The variable number was already assigned: 12
```

## Assignment

- [Assignment Operators](https://www.tutorialspoint.com/cprogramming/c_assignment_operators.htm)

Assignment operators use the Infix style. The values of the right operand are assigned to the left operand. 

Numbers 1 - 6 are straight forward assignment operators. 7 & 8 are shift AND assignment operators. 9 - 11 are **Bitwise** assignment operators. 

1. `=` - Assignment
2. `+=` - Add AND assignment
3. `-=` - Subtract AND assignment
4. `*=` - Multiply AND assignment
5. `/=` - Division AND assignment
6. `%=` - Modulus AND assignment
7. `<<=` - Left shift AND assignment
8. `>>=` - Right shift AND assignment
9. `&=` - Bitwise AND assignment
10. `^=` - Bitwise exclusive OR and assignment
11. `|=` - Bitwise inclusive OR and assignment

You would typically use these operators to store calculated values into variables without writing out the calculation yourself.

## Arithmetic

- [Arithmetic Operators](https://www.tutorialspoint.com/cprogramming/c_arithmetic_operators.htm)

Most of the arithmetic operators use the Infix style to perform calculations on two operands. Numbers 6 and 7 can use either the Prefix or Postfix style.

1. `+` - Addition
2. `-` - Subtraction
3. `*` - Multiplication
4. `\` - Division
5. `%` - Modulus
6. `++` - Increment
7. `--` - Decrement

```c
int num = 1;

--num;

num++;
```

What is the difference in the increment/decrements 'fix' style? Running the operator before the variable `num` ensures the value incremented/decremented is assigned and returned right away.

Running the increment/decrement operator **after** the variable `num` will return the original value of the variable, and then assign the new value. 

## Logical

- [Logical Operators](https://www.tutorialspoint.com/cprogramming/c_logical_operators.htm)

Logical operators use the Infix style, except number 3, which uses the Prefix style. These are boolean operators that return a `true` or `false` value. They evaluate the two operands and run them against each other to test equality in some way.

1. `&&` - Logical AND (Returns true if both operands are true )
2. `||` - Logical OR (Returns true if only one operand is true)
3. `!` - Logical NOT (Returns the inverse logical state of the operand it is being evoked on) 

## Relational

- [Relational Operators](https://www.tutorialspoint.com/cprogramming/c_relational_operators.htm)

Relational operators also use the Infix style. 

1. `==` - if both operands are equal, return true.
2. `!=` - if the left operand is not equal to the right operand, return true
3. `>` - if the left operand is greater than the right operand, return true
4. `<` - if the left operand is less than the right operand, return true
5. `>=` - if the left operand is greater than or equal to the right operand, return true
6. `<=` - if the left operand is less than or equal to the right operand, return true

## Bitwise

- [Bitwise Operators](https://www.tutorialspoint.com/cprogramming/c_bitwise_operators.htm)

Bitwise operators are a bit different. These operators operate on the bits in integer values. They're also different from Logical operators, as Logical operators will use two of these symbols whereas Bitwise will only use one.

They all use the Infix style, except for number 4. Number 4 uses the Postfix style.

1. `&` - Binary AND
2. `|` - Binary OR
3. `^` - Binary XOR
4. `~` - Binary One's Complement
5. `<<` - Binary Left Shift
6. `>>` - Binary Right Shift

---

I will go more in-depth on bitwise operators, operator precedence, plus other operators like cast and `sizeof` in the next few entries of this series. Thank you for reading!