C Operators – Logical, Arithmetic, Relational – POFTUT

# 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 operator is `+` as expected. In the following example two numbers are added and assigned to a variable.

`int a= 12+12;`

OR

`int a= b + c;`

## Subtract

As usual as subtraction we know which is  `-`

`int a = b - c;`

OR

`int a = 8 -4;`

## Multiply

Multiply operation can be done with `*` operator.

`int a = b * c;`

OR

`int a = 2 * 4;`

## Divide

Divide operation can be done with `/` . The result can not decimal.

`int a = b / c;`

OR

`int a = 4 / 2;`

## Modulus

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

`int a = b % c;`

OR

`int a = 10 / 7;`

## Increment

Increment operator will increment given variable only.

`a++`

OR

`++a`

## Decrement

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

`a--`

OR

`--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 `==`

```int a=12, b=12;

(a == b) //wil return true```

`(a == b)` will return true

```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 `!=`

```int a=12, b=11;

(a != b) //wil return true```

`(a != b)` will return true

```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 `>`

```int a=5, b=10;

a > b```

In this situation `a>b` will return false because `b` is greater than `a`

```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 <

```int a=5, b=10;

a < b```

In this situation `a<b` will return true because `b` is lower than `a`

```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

```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

```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 `&&`

```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 `||`

```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

```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.

```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.

```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.

```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

```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.

```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.

```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.

`int a = 3; // Variable a will be 3`

[rps-include post=6557]