Operators can be defined as symbols that help in performing some mathematical and logical operations. They are used to manipulate data and variables.

**Diag-1: Operator**

**For better understanding, please refer to these posts before moving further.**

**Types of operators in C:**

C operators can be classified into the following types:

- Arithmetic operator
- Assignment operator
- Bitwise operator
- Logical operator
- Relational operator
- Conditional operator
- Special operator
- Increment-decrement operator

**Arithmetic operators:**

C supports all basic arithmetic operators. The table below shows all the basic arithmetic operators.

Operator | Description | Examples |

+ | Adds two operands | a = 5 + 4 |

– | subtracts the second operand from the first | a = 8 – 5 |

* | Multiply two operands | a = 5 * 7 |

/ | divides numerator by denominator | a = 8 / 2 |

% | The remainder of the division | 8 %2 = 0 |

++ | Increment operator – increase integer value by 1 | a = 8 a++ = 9 |

— | decrement operator- decreases integer value by 1 | a = 8 a– = 7 |

**Assignment operators:**

Assignment operators supported by C language are as follows:

Operators | Description | Examples |

= | assign values from right side operands to left side operands | a = b |

+= | Add the right operand to the left operand and assign the result to the left operand | a+=b is same as a = a + b |

-= | subtract right operand to left operand and assign the result to left operand | a-=b is same as a= a – b |

*- | multiply left operand with the right operand and assign the results to the left operand | a*=b is same as a= a * b |

/= | divide left operand with the right operand and assign the result to the left operand | a/=b is same as a= a / b |

%= | Calculate modules using operands and assign the results to the left operand | a%=b is same as a= a % b |

**Bitwise operator:**

Bitwise operators perform manipulation on the bits of the data that is the manipulation of data at bit level. These operators also perform shifting bits from right to left. They do not apply to float or double data types.

Operator | Description |

& | Bitwise AND |

| | Bitwise OR |

^ | Bitwise exclusive OR (XOR) |

<< | left shift |

>> | right shift |

**Truth Table:**

a | b | a & b | a | b | a ^ b |

0 | 0 | 0 | 0 | 0 |

0 | 1 | 0 | 1 | 1 |

1 | 0 | 0 | 1 | 1 |

1 | 1 | 1 | 1 | 0 |

Bitwise shift operator, shift the bit value. The left operand specifies the value to be shifted and the right operand specifies the number of positions that the bits in the value have to be shifted. Both operands have the same precedence.

**Example:**

```
a = 00001000 // 8 in decimal
b = 2
a << b = 01000000 // 64 in decimal
a >> b = 00000001 // 1 decimal
```

**Note:**

- Left shifting a number by x bit is equivalent to multiplying that number by 2^ x.
- Right shifting a number by x bits is equivalent to dividing that number by 2^x

**Logical operators:**

C language supports 3 logical operators.

a | b | operator | Description | Examples |

1 | 0 | && | Logical AND | (a && b ) is false |

1 | 0 | || | Logical OR | (a || b) is true |

1 | 0 | ! | Logical Not | (!a) is false, (!b) is true |

**Relational operator**

The following table shows the relational operator supported by C

Operator | Description |

== | Check if two operands are equal, return 1 if equal else return 0 |

!= | Check if the two operands are not equal |

> | Check if the left hand side operand is greater than the right hand operand |

< | Check if the left hand side operand is smaller than the right hand operand |

>= | Check if the left hand side operand is greater than or equal to the right hand operand |

<= | heck if the left hand side operand is smaller than or equal to the right-hand operand |

**Conditional Operator**

- The conditional operators in C language also known as Ternary or ?: Operator
- It is equivalent to writing the if condition which is used in decision-making, but using this conditional operator we turn the if condition statement into a short and simple operator.

**Syntax:**

expression 1 ? expression 2: expression 3

**Explanation:**

- The question mark
**“?”**in the syntax represents the**if**part. - The first expression (expression 1) generally returns either true or false, based on which it is decided whether (expression 2) will be executed or (expression 3)
- If (expression 1) returns true then the expression on the left side of
**” : “**i.e (expression 2) is executed. - If (expression 1) returns false then the expression on the right side of
**” : “**i.e (expression 3) is executed.

#include <stdio.h> int main() { int num1 = 30; int num2 = 40; (num1 > num2) ? printf("\n num1 is greater") : printf("\n num2 is greater"); return(0); }

**OUTPUT:**

num2 is greater **…Program finished with exit code 0Press ENTER to exit console.**

**Diag-2: Conditional operator**

**Special Operator:**

C has few specials operators like as follows:

Operator | Description | Examples |

sizeof | returns the size of a variable | sizeof(x) return size of variable x |

& | Returns the address of a variable | &x; return address of the variable |

* | Pointer to a variable | *x; will be pointer to variable x |

. | selection of members of a structure via object (dot operator) | obj.mem; |

-> | Selection of members of the structure which is referenced by the pointer | obj->mem |

**Increment and Decrement Operators:**

This one as the name suggests will help us in incrementing and decrementing the values. Both these operators are further classified into two types:

- Pre-Increment / Post Increment
- Pre- Decrement/ post Decrement

**Pre-Increment and Pre-Decrement**:

This is used to increment and decrement the value of the variable before using the variable.

**Example:**

int a = 10;

++a; ==> pre increment

–a; ==> pre Decrement**Sample Code:**

#include <stdio.h> int main() { int a = 10, b = 11, x , y; x = ++a; y = --b; printf ("\n The value of x is :%d", x); printf ("\n The value of y is :%d", y); return 0; }

**OUTPUT:**

The value of x is :11

The value of y is : 9

**Post increment and post Decrement:**

In this case, the value of the variable is incremented or decremented once the expression is executed in which post-increment or decrement is used.

**#include <stdio.h>
int main()
{
int a = 10, b = 11, x , y;
x = a++;
y = b--;
printf ("\n The value of x is :%d", x);
printf ("\n The value of y is :%d", y);
return 0;
}**

**Output:**

The value of x is: 10

The value of y is: 11

**Relevant Posts:**

Categories: C Language

## Leave a Reply