# C Operators Tutorial

In this C tutorial we learn about operands, lvalues and rvalues. We also learn about the arithmetic, assignment, relational, logical, unary and ternary operators.

## What is an operator

An operator is one or more symbols that have a special meaning, in certain contexts, to the compiler.

For example, the **+** symbol is the addition operator and will perform addition arithmetic on two numeric values.

The following types of operators are available in C.

- Arithmetic operators
- Assignment operators
- Relational operators
- Logical operators
- Bitwise operators
- Increment/Decrement operator
- Ternary conditional operator
- Other operators

## Operands, Lvalue & Rvalue

There are two kinds of expressions in C, **Lvalues** and **Rvalues**. Lvalues and Rvalues are also known as the left operand and the right operand respectively, or operands in general.

An lvalue is the operand that can have a value assigned to it. It’s allowed to appear on either the left or the right side of an assignment.

```
// lvalue (num1) may be
// on the left of the =
int num1 = 10;
// lvalue may also be on
// the right of the =
int num2 = num1;
```

An rvalue is the operand that cannot be assigned a value. It may only appear on the right side of an assignment.

```
// rvalue (10) may only
// be on the right of the =
int num1 = 10;
// an rvalue cannot be
// on the left of the =
10 = 10;
```

## Arithemtic operators

Arithmetic operators allow basic mathematical arithmetic to be performed on operands.

The following table lists the arithmetic operators available in C.

Operator | Description |
---|---|

+ | Add the right operand to the left operand. |

* | Multiply the left operand with the right operand. |

/ | Divide the left operand by the right operand. |

% | Modulo. Divide the left operand by the right operand and return the remainder of the division. |

## Assignment operators

An assignment operator is used to assign the operand on the right, to the operand on the left.

The following table lists the assignment operators available in C.

Operator | Description | Example | Same as |
---|---|---|---|

= | Assign | a = b | a = b |

+= | Add and assign | a += b | a = a + b |

-= | Subtract and assign | a -= b | a = a - b |

*= | Multiply and assign | a *= b | a = a* b |

/= | Divide and assign | a /= b | a = a / b |

%= | Modulo and assign | a %= b | a = a % b |

## Relational operators

Relational operators evaluate the relationship between operands and are primarily used in conditional and iteration control flow.

The following table lists the relational operators available in C.

Operator | Description | Example |
---|---|---|

== | Equal to | 1 == 1 is true |

> | Greater than | 2 > 1 is true |

< | Less than | 3 < 1 is false |

!= | Not equal to | 2 != 3 is true |

>= | Greater than or equal to | 2 >= 1 is true |

<= | Less than or equal to | 5 <= 5 is true |

## Logical operators

Logical operators are used to combine conditional evaluations. The result is either true or false.

The following table lists the logical operators available in C.

Operator | Name | Description |
---|---|---|

&& | And | Returns true if both expressions evaluate to true. |

|| | Or | Returns true if one or the other expression evaluate to true. |

! | Not | Returns true if the expression immediately following it not true. |

## Increment / Decrement (unary) operators

Incremental operators are used to increase a value incrementally, and decremental operators are used to decrease a value.

These operators are what is known as unary operators and as such only require one operand.

The following table lists incremental and decremental operators available in C.

Operator | Name | Description |
---|---|---|

++a | Pre-increment | Increment by one, then return __a__. |

a++ | Post-increment | Return __a__, then increment by one. |

--a | Pre-decrement | Decrement by one, then return __a__. |

a-- | Post-decrement | Return __a__, then decrement by one. |

## The ternary operators

The ternary operator is a conditional operator that functions as a replacement for a conditional if/else expression with only single execution statements.

Operator | Description |
---|---|

? : | Replaces a simple conditional if/else statement. |

```
int num = 10;
if (num > 5)
{
printf("Num > 5");
}
else
{
printf("Num < 5");
}
```

The normal if/else statement in the example above, will become the following expression when converted to a ternary.

```
int num = 10;
// condition if true if false
num > 5 ? printf("Num > 5") : printf("Num < 5");
```

*We look at the ternary operator again in the lesson on conditional control with if/else statements*.

## Other operators

C supports more operators in addition to all of the operators mentioned above.

Operator | Name | Description |
---|---|---|

. | Dot notation | Access the members of structures and unions. |

-> | Arrow notation | Access the members of structures and unions. |

* | Dereference | Dereference the value of a pointer variable. |

& | Address of | Get the actual memory address of a variable. |

sizeof() | Size of operator | Get the size of a data type. |

A blank space is considered to be the separator operator. It separates programming elements from each other.

As an example, a separator will separate a keyword from another keyword, or a keyword from an identifier.

## Summary: Points to remember

- Operators are special symbols in C that allow us to perform operations.
- Operands are the
**lvalue**and**rvalue**expressions in C.