We have learned about variables and constants so far. In this part of C tutorial, let us take look at what is meant by an operator and what are different types of operators available in C programming language.

## Operator Basics

Operators in C language are symbols which tells the compiler to perform some mathematical calculations or logical operations (*we will look at in a while*) on the variables and constants. For example, consider the mathematical operation "`10 + 2 - a"`

. Here '+' and '-' are called operators and the values on which operators work ('10', '2', and 'a') are known as operands. C programming language has wide variety of operators which are categorised into different groups based on the type of operations they perform.

## Operator Arity

Arity | Description |
---|---|

Unary Operator | Operates on single operand. |

Binary Operator | Operates on two operands, For example: '/' (used for division). |

Ternary Operator | Operates on three operands. Example : conditional operator (we will discuss about it in this chapter). |

n-ary Operator | Operators that operate on more than 3 operands. |

## Types of Operators

Type of Operator | Description |
---|---|

Arithmetic operator | Arithmetic operators are used to perform mathematical operations. |

Relational operator | Relational operators are used to perform comparison operations. |

Logical operator | Logical operators are used to perform basic operation of boolean algebra ('AND', 'OR', and 'NOT'). |

Bitwise operator | Bitwise operators are used to perform bit wise manipulation on operands. |

Assignment operator | Assignment operator is used to assign value of the second operand to first. |

Conditional operator | Conditional operators operates on a condition and return result based on the truth value of condition. |

Now let's take a look at different types of operators and it's examples in detail.

## Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication and division. There are five arithmetic operators available in C (`+, -, *, /, %`

). All arithmetic operators are binary operators, ie; they operate on two operands to produce the result.

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

+ | Addition Operator | '+' Operator adds two values. This operator works on integer, float and character variables. | `20 + 10` return 20 |

- | Subtraction Operator | '-' Operator produces a result after subtracting two values. It also works on integer, float, character variables. | `20 - 10` returns 10 |

* | Multiplication Operator | '*' Operator produces result after multiplication of operands. | `20 * 10` returns 200 |

/ | Division Operator | '/' Operator produces result of division of first operand by second. | `20 / 10` returns 2 |

& | Modulus Operator | '%' Operator generates the remainder after integer division. | `25 % 10` returns 5 |

### Examples of Arithmetic Operators

#include <stdio.h> int main() { int a = 25, b = 10; printf("Sum: %d\n", a + b); printf("Difference: %d\n", a - b); printf("Product: %d\n", a * b); printf("Quotient: %d\n", a / b); printf("Remainder: %d\n", a % b); return 0; } Output: Sum: 35 Difference: 15 Product: 250 Quotient: 2 Remainder: 5

## Increment and Decrement operators ( ++, --)

There are two special arithmetic operators called increment and decrement operators available C. They are unary operators unlike all other arithmetic operators. The operators are denoted as : '++' (*increment operator*), '--' (*decrement operator*).

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

++ | Increment Operator | Increments the value of operand by 1 | `a++` : This is equivalent to a = a + 1 |

-- | Decrement Operator | Decrements the value of operand by 1 | `a--` : This is equivalent to a = a - 1 |

### Variants of Increment and Decrement Operators - Pre Increment, Post Increment, Pre Decrement and Post Decrement

There are two different variants of increment and decrement operators known as pre increment, post increment, pre decrement and post decrement operators.

**Pre Increment Operators**: When '++' is used as prefix of the operand, it's called pre increment operator. Pre increment operator will increment the value of operand before using it in the expression.**Post Increment Operators**: When '++' is used as postfix of the operand, it's called post increment operator. Post increment operator will increment the value of operand after using it in the expression. ie; The current expression will use non-incremented value of operand.**Pre Decrement Operators**: When '--' is used as prefix of the operand, it's called pre decrement operator. Pre decrement operator will decrement the value of operand before using it in the expression.**Post Decrement Operators**: When '--' is used as postfix of the operand, it's called post decrement operator. Post decrement operator will decrement the value of operand after using it in the expression. ie; The current expression will use non-decrement value of operand.

Examples of pre increment, post increment, pre decrement and post decrement operators are given below

#include <stdio.h> int main(void) { int a = 5; // Pre increment operator. Value of 'a' will be incremented first and then // used in the expression. So b is assigned value after a gets incremented. int b = ++a; printf("a = %d, b = %d\n", a, b); a = 5; // Post increment operator. The value of 'a' will be incremented only after // it gets used in the expression. That means, first b will get assigned the // value of 'a' and then value of 'a' will be incremented . b = a++; printf("a = %d, b = %d\n", a, b); a = 5; // Pre decrement operator. Similar to pre increment operator, pre decrement // operator decrements the value of operand before using it in the expression. b = --a; printf("a = %d, b = %d\n", a, b); a = 5; // Post decrement operator. Similar to post increment operator, post decrement // operator decrements the value of operand after using it in the expression. b = a--; printf("a = %d, b = %d\n", a, b); return 0; } Output: a = 6, b = 6 a = 6, b = 5 a = 4, b = 4 a = 4, b = 5

## Assignment Operator

Assignment operator assigns value of the expression on the right side to left side variable. The base assignment operator is '='. In C, you can use this operator like the following `variable = expression`

Here variable can be any kind of a variable and expression can be a simple constant, another variable or may be a more complex expression, like a formula. The value of the expression will be evaluated and assigned to the variable. But, there are some things to note about the assignment operator.

If the value of the expression on the right side is not same data type as variable on the left, then the value of the expression will be converted to same data type as the variable. Suppose, the expression is a floating point number and the variable is an integer. Then, the floating point number will be converted to integer and then will be assigned to the variable. It is better to use same data types on both sides unless it is a must, else it may lead to information loss.

C also allows multiple assignment statement like `variable1 = variable2 = ... = variableN = expression.`

This means, the value of the expression will be assigned to '*variableN*', which in turn will be assigned to the variable left of '*variableN*' and it propagates till '*variable1*'. So, after the execution of multiple assignment operation, all the variables will have the same value.

Apart from the base assignment operator explained above, C also has different variants of the same assignment operator. They are ` '+=', '-=', '*=', '/=', '%='`

. The behaviour of these operators is explained below.

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

+= | Add and Assignment Operator | This operator perform addition of operand on left side with operand right side and the result will be assigned to operand at left side. | ` a += 10` : This is equivalent to `a = a + 10` |

-= | Subtract and Assignment Operator | This operator subtracts the operand on right side from the operand on left side and the result will be assigned to left side operand. | ` a -= 10` : This is equivalent to `a = a - 10` |

*= | Multiplication and Assignment Operator | This operator multiplies the operand on right side with the operand on left side and the result will be assigned to left side operand. | ` a *= 10` : This is equivalent to `a = a * 10` |

/= | Division and Assignment Operator | This operator divides the operand on left side with the operand on right side and the result will be assigned to left side operand. | ` a /= 10` : This is equivalent to `a = a / 10` |

%= | Modulus and Assignment Operator | This operator computes the modulus of left side operand by right side operand and the result will be assigned to left side operand. | ` a %= 10` : This is equivalent to `a = a % 10` |

### Examples of Assignment Operator

#include <stdio.h> int main() { int a = 25, b = 10; a += b; printf("'a' after a += b: %d\n", a); a = 25, b = 10; a -= b; printf("'a' after a -= b: %d\n", a); a = 25, b = 10; a *= b; printf("'a' after a *= b: %d\n", a); a = 25, b = 10; a /= b; printf("'a' after a /= b: %d\n", a); a = 25, b = 10; a %= b; printf("'a' after a \%= b: %d\n", a); return 0; } Output: 'a' after a += b: 35 'a' after a -= b: 15 'a' after a *= b: 250 'a' after a /= b: 2 'a' after a %= b: 5

**Also Read** - Top C Interview Questions and Answers for Freshers & Advanced C Interview Questions and Answers

## Relational operator

Relational operators are binary operators(operates on two operands) and are used to relate or compare two operands. There are four relational operators in C (i.e `<, <=, >, >=`

). If the relationship between the operands is correct, it will return 1 and returns 0 otherwise.

Apart from four relational operators, C has two equality operator (== and !=) as well for comparing operands. Now let's take a look at different relational and equality operators and how they operate on the operands.

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

< | 'Less than' operator | Checks if the first operand is less than second operand and returns 1 if it's true, else returns 0 | `10 < 5` returns 0 and `5 < 10` returns 1 |

<= | 'Less than or equals to' operator | Checks if the first operand is less than or equals to second operand and returns 1 if it's true, else returns 0 | `10 <= 10` returns 1 and `10 <= 5` returns 0 |

> | 'Greater than' operator | Checks if the first operand is greater than second operand and returns 1 if it's true, else returns 0 | `10 > 5` returns 1 and `5 > 10` returns 0 |

>= | 'Greater than or equals to' operator | Checks if the first operand is greater than or equals to second operand and returns 1 if it's true, else returns 0 | `10 >= 10` returns 1 and `5 >= 10` returns 0 |

== | Equality operator | Checks if the two operands are equal are returns 1 if it's true, else returns 0 | `10 == 10` returns 1 and `10 == 1` returns 0 |

!= | Non-equality operator | Checks if the two operands are equal are returns 1 if they are not equal, else returns 0 | `10 != 10` returns 0 and `10 != 1` returns 1 |

### Examples of Relational Operator

#include <stdio.h> int main() { int a = 25, b = 10; printf("%d < %d : %d\n", a, b, a < b); printf("%d <= %d: %d\n", a, b, a <= b); printf("%d > %d : %d\n", a, b, a > b); printf("%d >= %d: %d\n", a, b, a >= b); printf("%d == %d: %d\n", a, b, a == b); printf("%d != %d: %d\n", a, b, a != b); return 0; } Output: 25 < 10 : 0 25 <= 10: 0 25 > 10 : 1 25 >= 10: 1 25 == 10: 0 25 != 10: 1

**Also Read** - Top Embedded C Interview Questions and Answers & C++ Interview Questions and Answers

## Logical operator

We've learned about relational operators, using which we can do comparisons. What if you need to combine two relational expressions, for example,* if you want to check if a number is greater than 10 and less than 20?* You can use logical operators for that purpose. There are three logical operators in C language, `&&, ||, !`

. Details and examples of logical operators are given below

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

&& | 'Logical AND' Operator | 'AND' operator returns true if both the operands it operates on evaluates to true (non-zero), else return false | `a && b` returns true if both a and b are non-zero |

|| | 'Logical OR' Operator | 'OR' operator returns true if any of the operands it operates on evaluates to true (non-zero), else return false | `a || b` returns true if either a or b are non-zero |

! | 'Logical NOT' Operator | 'NOT' operator is a unary operator (it operates only on one operand). It returns true if the operand is false and returns false if the operand is true | `!a` returns true if a is false. Else returns false |

### Examples of Logical Operators

#include <stdio.h> int main(void) { int a = 5, b = 6; printf("(a == 5) && (b == 7) : %d\n", (a == 5) && (b == 7)); printf("(a == 5) || (b == 7) : %d\n", (a == 5) || (b == 7)); printf("!(a == 5) : %d", !(a == 5)); return 0; } Output: (a == 5) && (b == 7) : 0 (a == 5) || (b == 7) : 1 !(a == 5) : 0