**What is the Operator?**

The **operator** is a symbol that tells the computer to perform certain mathematical or relational manipulations over some data. The data items on which operator acts are called **operands**. The rich set of the operator is available in enabling us to write efficient and concise programs.

**There are a few types of operators in C:**

- Logical Operators,
- Bitwise Operators,
- Special Operators,
- Relational Operators,
- Arithmetic Operators,
- Conditional Operators,
- Assignment Operators,
- Compound Assignment Operator,
- Increment and Decrement Operators.

**Logical operators:**

**Logical operators** are used to evaluating one single expression consists of two or more relational expressions.

- ! (NOT)
- ||(logical OR)
- && (logical AND)

**Logical NOT:**

In **Logical NOT** Exp can be changed from false to true, or form true to false.

Expressions | Result |
---|---|

! (True) | False |

! (False) | True |

**Logical OR:**

The **Logical OR **operator** **combines two expressions. The logical **OR** operator evaluates to True which is 1.

In other words, if the condition of any expression is true then the combined Exp will give the result as True otherwise False.

Example. Exp1 || Exp2

Expressions | Result |
---|---|

F || F | False |

F || T | True |

T || F | True |

T || T | True |

**Logical AND:**

A compound expression is true when two conditions (expressions) are true to write both conditions the operator **(&&)** is used.

Example.

Exp && Exp

In the case of **AND operator** is is 1st Exp is true then 2nd Exp will be evaluated if the 1st Exp is false then 2nd Exp is not evaluated at all.

Expressions | Result |
---|---|

F && F | False |

F && T | False |

T && F | False |

T && T | True |

**Equality Operators:**

- == (equal to)
- != (Not equal to)

**Example of logical operators:**

#include <stdio.h> #include <stdlib.h> int main() { int i=8, j=3, k, l; k=i && j; l=i || j; printf("Value of k=%d\n", k); printf("Value of l=%d", l); return 0; }

**Output:**

```
Value of k=1
Value of l=1
```

**Arithmetic Operators:**

An **arithmetic operator **is used to perform the basic arithmetic operations like addition, subtraction, multiplication, division, modulus, etc.

Operator | Meaning |
---|---|

+ | Addition |

– | Subtraction |

* | Multiplication |

/ | Division |

% | Modulus |

**Program of arithmetic operators**:

#include<stdio.h> #include<stdlib.h> int main() { int a = 9,b = 5, c; c = a+b; printf("Addition a+b = %d \n",c); c = a-b; printf("Subtraction a-b = %d \n",c); c = a*b; printf("Multiplication a*b = %d \n",c); c = a/b; printf("Division a/b = %d \n",c); c = a%b; printf("(Modulus) Remainder when a divided by b = %d",c); return 0; }

**Output:**

```
Addition a+b = 14
Subtraction a-b = 4
Multiplication a*b = 45
Division a/b = 1
(Modulus) Remainder when a divided by b = 4
```

We operate an operation of an **arithmetic operator.** In normal calculation 9/5=1.8, but here we put integer variable and in integer variables compiler neglect the decimal points and show only value.

**Note:**

- The modulus operator(%)gives the remainder after an integer division.
- The modulus operator (%) can not be used with floating-point numbers.

**Relational Operators:**

**Relational operators** are used to comparing two operands. In other words, an expression containing relational operators evaluate to either true(1) or false(0).

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

== | Is equal to | 8 == 2 | False |

!= | Is not equal to | 8 != 2 | True |

< | Less than | 8 < 2 | False |

> | Greater than | 8 > 2 | True |

<= | Less than or equal to | 8 <= 2 | False |

>= | Greater than or equal to | 8 >= 2 | True |

**Example of Relational Operators:**

#include <stdio.h> #include <stdlib.h> int main() { int a=7, b=7, c=10; printf("For %d == %d: The output is: %d \n", a,b,a == b); // condition is true printf("For %d == %d: The output is: %d \n", a, c, a == c);// condition is false printf("For %d != %d: The output is: %d \n", a, c, a != c);// condition is true printf("For %d != %d: The output is: %d \n", a, b, a != b);// condition is false printf("For %d > %d: The output is: %d \n", c, b, c > b);// condition is false printf("For %d > %d: The output is: %d \n", a, c, a > c);// condition is false printf("For %d < %d: The output is: %d \n", a, b, a < b); // condition is false printf("For %d < %d: The output is: %d \n", a, c, a < c);// condition is true printf("For %d >= %d: The output is: %d \n", a, b, a >= b);// condition is true printf("For %d >= %d: The output is: %d \n", a, c, a >= c);// condition is false printf("For %d <= %d: The output is: %d \n", a, b, a <= b);// condition is true printf("For %d <= %d: The output is: %d", a, c, a <= c);// condition is true return 0; }

**Output:**

```
For 7 == 7: The output is: 1 //1(true)
For 7 == 10: The output is: 0 //(false)
For 7 != 10: The output is: 1 //1(true)
For 7 != 7: The output is: 0 //(false)
For 10 > 7: The output is: 1 //1(true)
For 7 > 10: The output is: 0 //(false)
For 7 < 7: The output is: 0 //(false)
For 7 < 10: The output is: 1 //1(true)
For 7 >= 7: The output is: 1 //1(true)
For 7 >= 10: The output is: 0 //(false)
For 7 <= 7: The output is: 1 //1(true)
For 7 <= 10: The output is: 1 //1(true)
```

When the program statement is satisfied(it’s true), and compiler gives output value 1 in the program to show program is satisfied, but when the program statement did not satisfied(it’s false), and compiler gives output value 0 in the program to show program did not satisfied.

**Assignment Operators:**

An **Assignment operator** are used to assign some expressions in a variable.

In other words, an **assignment operator** is used to performing arithmetic operations while assigning a value to a variable.

Example. a=25

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

+= | Addition Assignment |

-= | Subtraction Assignment |

*= | Multiplication Assignment |

&= | Bitwise AND assign |

%= | Modulus Assignment |

|= | Bitwise inclusive OR and assign |

>>= | Right shift and assign |

/= | Division Assignment |

^= | Bitwise exclusive OR and assign |

<<= | Left shift and assign |

**Example of Assignment Operator:**

#include <stdio.h> #include <stdlib.h> int main() { int i=5; printf("Value of i=%d\n", i+=3); printf("Value of i=%d\n", i*=3); printf("Value of i=%d", i!=3); return 0; }

**Output:**

```
Value of i=8
Value of i=24
Value of i=1
```

**The use of assignment operators has advantages.**

- The statement is more compact and easier to read.
- The statement is more efficient.

**Compound Assignment Operator:**

The **compound assignment** operator is used as an alternate the combine a binary operation with assignment operation.

**Example of Compound assignment operator:** Increment

#include <stdio.h> #include <stdlib.h> int main() { int i=5; i=i+10; printf("Value of i=%d\n", i); printf("Value of i=%d\n", i+=10); return 0; }

**Output:**

```
Value of i=15
Value of i=25
```

**Increment and Decrement Operator:**

**Increment and Decrement** operators are used to add and subtract the value of a variable with some specified value respectively.

**Example of Increment Operator (++):**

#include <stdio.h> #include <stdlib.h> int main() { int i=5; printf("Value of i through post-increment i=%d\n", i++); printf("Value of i=%d\n", i); printf("Value of i through pre-increment i=%d", ++i); return 0; }

**Output:**

```
Value of i through post-increment i=5
Value of i=6
Value of i through pre-increment i=7
```

**Example of Decrement Operator (–):**

#include <stdio.h> #include <stdlib.h> int main() { int a = 5; a--; printf("Decrement is: %d",a); return 0; }

**Output:**

`Decrement is: 4`

**Conditional Operator:**

Ternary operators are called **conditional operators.** Ternary operators are ? and : operators since they take three arguments.

- expression 1 (?)
- expression 2 (:)
- expression 3 (;)

If expression 1 is true, then the value returned will be expression 2, otherwise, the value returned will be expression 3.

**Example of Conditional Operators:**

#include <stdio.h> #include <stdlib.h> int main() { int i, j; printf("Enter the value of i="); scanf("%d", &i); j=(i>2?10:20); printf("Value of j=%d", j); return 0; }

**Output:**

**(First run: Value of i is less than 2)**

```
Enter the value of i=1
Value of j=20
```

(**Second run: value of i is greater than 2) **

```
Enter the value of i=3
Value of j=10
```

The j=(i>2?10:20); statement will store 10 in j if i is greater than 2, otherwise it will store 20 in j.

The equivalent if the statement will be,

if (i>2) j=10; else j=20;

**Bitwise Operators:**

**Bitwise operators** are used in bit operations to speed up our program or perform neat tricks. In C four types of bitwise logical operators are:

- Bitwise AND Operator
- Bitwise OR Operator
- Bitwise Complement Operator

**Bitwise AND (&) Operator:**

This operator is represented by **“&”**. The **&** operator operates on two operands while operation upon these two operands they are compared on a bit by bit basis, hence both the operand must be the same type (either char or int).

The **Bitwise AND** operations will be carried out between the two-bit patterns of the two operators.

The first bit (a) | The second bit (b) | Resultant (a+b) |
---|---|---|

0 | 0 | 0 |

1 | 0 | 0 |

0 | 1 | 1 |

1 | 1 | 1 |

Example: 10101010 and 11000011

The result will be 10000010

**Bitwise OR (|) Operator:**

The **bitwise OR operator** (|) frequency referred to simply as a bitwise OR operator. Its form is:

integer value | integer value

When the bitwise **OR** is applied to two integral operands, the binary representation of the converted values of the operands are compared bit by bit,

The first bit (a) | The second bit (b) | Resultant (a|b) |
---|---|---|

0 | 0 | 0 |

1 | 0 | 1 |

0 | 1 | 1 |

1 | 1 | 1 |

Example: 10101010 and 11100110

The result will be 11101110.

**Bitwise XOR Operator:**

The **XOR operator** is represented by “^” and also called Exclusive OR operator.

The OR operator returns 1, only one of the two-bit both bits are 1, whereas XOR returns 1 only. If one of the two bits is 1.

The first bit (a) | The second bit (b) | Resultant (a ^ b) |
---|---|---|

0 | 0 | 0 |

1 | 0 | 1 |

0 | 1 | 1 |

1 | 1 | 0 |

Example. 10101010 and 111001100

The result will be 01001100

**Bitwise Complement Operator:**

The **bitwise complement** operator has the form and yields the 1’s complement of the converted integral operand.

1-bit into 0-bit and vice versa. It is represented by (~).

First (b) | ~b’ |
---|---|

1 | 0 |

0 | 1 |

Example: b= 10101010 than ~b = 01010101

**Special Operators:**

**” &, *, . , ->, size of”**

**1. Unary operators**

- * (pointer operator)
- & (address operator)
- sizeof (size of operator)

**2. &** operators

**Example of pointer and address operator:**

#include <stdio.h> #include <stdlib.h> int main() { int i=7, *ptr; ptr=&i; printf("Value of i:%d\n",i); printf("Address of i:%d\n",&i); printf("Value at address of i:%d", *(&i)); return 0; }

**Output:**

```
Value of i:7
Address of i:6356728
Value at address of i:7
```

**Size of operator:**

The **sizeof** **operator** is used to give the direction to the compiler to preserve the memory size to the particular data type.

**Example of size of operator:**

#include <stdio.h> #include <stdlib.h> int main() { int i; float f; char ch; printf("Bytes used by char ch=%d Bytes\n",sizeof(ch)); printf("Bytes used by float f=%d Bytes\n",sizeof(f)); printf("Bytes used by integer i=%d Bytes\n",sizeof(i)); return 0; }

**Output:**

```
Bytes used by char ch=1 Bytes
Bytes used by float f=4 Bytes
Bytes used by integer i=4 Bytes
```

Previous Lecture:

Previous Lecture

**Data TypesEscape CharacterConditional statements-if-if-else**

If you have any problem in solving your questions, then join our **Telegram** group or search **@Learnsimply**, we will help you.

Please write comments if you find anything incorrect in the **Operators in C **lecture.