# C Operators – Logical, Arithmetic, Relational

[rps-include post=6557]

Operators are leg and arm of the C programming language. A lot of operations done with operators. Operators used to assign, sum, subtract, multiply, divide, remainder, increment, decrement … operations. We will look all of these operations in this chapter. Operators can be grouped like below.

- Arithmetic
- Relational
- Logical
- Bitwise
- Assignment
- Misc

# Arithmetic

Arithmetic is most popular usage of operators. Add, Subtract, Multiply, Divide, Modulus, Increment, Decrement operations can be done with arithmetic operators.

## Add

Add operator is `+`

as expected. In the following example two numbers are added and assigned to a variable.

1 |
int a= 12+12; |

OR

1 |
int a= b + c; |

## Subtract

As usual as subtraction we know which is `-`

1 |
int a = b - c; |

OR

1 |
int a = 8 -4; |

## Multiply

Multiply operation can be done with `*`

operator.

1 |
int a = b * c; |

OR

1 |
int a = 2 * 4; |

## Divide

Divide operation can be done with `/`

. The result can not decimal.

1 |
int a = b / c; |

OR

1 |
int a = 4 / 2; |

## Modulus

Modulus operator will return the remainder or module operation result of two value.

1 |
int a = b % c; |

OR

1 |
int a = 10 / 7; |

## Increment

Increment operator will increment given variable only.

1 |
a++ |

OR

1 |
++a |

## Decrement

Decrement operation is the reverse of increment. Given variable will be decremented by one with `--`

operator like below.

1 |
a-- |

OR

1 |
--a |

# Relational

Relational operators generally used to compare two variables about their value. Below relational operators can be found.

## Equal

Equal operator will check values of given variables. If these values are equal `true`

will be returned, if their values are not equal `false`

will be returned. Equal operator is expressed as `==`

1 2 3 |
int a=12, b=12; (a == b) //wil return true |

`(a == b)`

will return true

1 2 3 |
int a=12, b=11; (a == b) //wil return false |

`(a == b)`

will return false

## Not Equal

Not equal operator is used to compare variables and return true if they are different, returns false if they are same. Not equal operator is expressed as `!=`

1 2 3 |
int a=12, b=11; (a != b) //wil return true |

`(a != b)`

will return true

1 2 3 |
int a=12, b=12; (a != b) //wil return false |

`(a == b)`

will return false

## Greater

Greater operator is used to test id one variable is greater then other. Greater operator can be expressed with `>`

1 2 3 |
int a=5, b=10; a > b |

In this situation `a>b`

will return false because `b`

is greater than `a`

1 2 3 |
int a=15, b=10; a > b |

In this situation `a>`

b will return true because a is greater than b

## Lower

Lower operator is used to test id one variable is lower then other. Lower operator can be expressed with <

1 2 3 |
int a=5, b=10; a < b |

In this situation `a<b`

will return true because `b`

is lower than `a`

1 2 3 |
int a=15, b=10; a < b |

In this situation `a>`

b will return false because a is lower than b

## Equal or Greater

Equal or Greater operator is similar to greater operator with addition to check equality with greatness. Equal or great will check if variables are equal or greater

1 2 3 |
int a=10, b=10; a >= b |

This will return true because two variables are equal which is acceptable by our operator.

## Equal or Lower

Equal or Lower operator is similar to lower operator with addition to check equality with lowness. Equal or lower will check if variables are equal or lower

1 2 3 |
int a=10, b=10; a <= b |

This will return true because two variables are equal which is acceptable by our operator.

# Logical

Logical operators are important part of decition making. They are generally used with if, for, while keywords where we will look next chapters in detail.

## And

And operator is a logical operator. And operator is expressed with `&&`

1 2 3 4 5 6 7 |
true && true //is true true && false //is false false && true //is false false && false //is false |

## Or

Or is a logical operator. Or operator can be expressed with `||`

1 2 3 4 5 6 7 |
true || false //is true true || true //is true false || true //is true false || false //is false |

## Not

Not operator is used with logic `true`

and `false`

. Not will revert given value

1 2 3 |
bool a = true; !a // is equal false |

# Bit-wise

Bitwise operators are used to change variables with binary operations.

## Binary And

This operation will `And`

two variables in a binary manner with their binary presentations.

1 2 3 4 5 |
int a=1; // Binary presentation is 00000001 int b=3; // Binary presentation is 00000011 int result = a & b; // Result will be 00000001 |

## Binary Or

This operation will Or two variables in a binary manner with their binary presentations.

1 2 3 4 5 |
int a=1; // Binary presentation is 00000001 int b=3; // Binary presentation is 00000011 int result = a | b; // Result will be 00000011 |

## Binary Xor

This operation will `Xor`

two variables in a binary manner with their binary presentations.

1 2 3 4 5 |
int a=1; // Binary presentation is 00000001 int b=3; // Binary presentation is 00000011 int result = a ^ b; // Result will be 00000010 |

## Binary One Complement

This operator will complement variables in binary form

1 2 3 |
int a=1; // Binary presentation is 00000001 int result = ~a ; // Result will be 11111101 |

## Binary Left Shift

This operator will shift specified step left the binary presentation of variable.

1 2 3 |
int a=1; // Binary presentation is 00000001 int result = a << 2 ; // Result will be 00000100 |

## Binary Right Shift

This operator will shift specified step right the binary presentation of variable.

1 2 3 |
int a=1; // Binary presentation is 00000001 int result = a >> 2 ; // Result will be 01000000 |

# Assignment

Assignment is a popular operator used most of time for numerical operations.

1 |
int a = 3; // Variable a will be 3 |

[rps-include post=6557]