Learn one of the most powerful programming languages in the world and become a rockstar developer.

In this tutorial, you will learn about different operators in C programming with the help of examples.

An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C language is rich in built-in operators and provides the following types of operators −

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

We will, in this chapter, look into the way each operator works.

An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division etc on numerical values (constants and variables).

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

+ | addition or unary plus | A + B = 30 |

− | subtraction or unary minus | A − B = -10 |

* | multiplication | A * B = 200 |

/ | Division | B / A = 2 |

% | remainder after division (modulo division) | B % A = 0 |

++ | Increment operator increases the integer value by one. | A++ = 11 |

-- | Decrement operator decreases the integer value by one. | A-- = 9 |

// Working of arithmetic operators #include <stdio.h> int main() { int a = 4,b = 2, c; c = a+b; printf("a+b = %d \n",c); c = a-b; printf("a-b = %d \n",c); c = a*b; printf("a*b = %d \n",c); c = a/b; printf("a/b = %d \n",c); c = a%b; printf("Remainder when a divided by b = %d \n",c); c = ++a; printf("The value of a after increment by 1 = %d \n",c); c = --b; printf("The value of b after decrement by 1 = %d \n",c); return 0; }

Output :

a+b = 6

a-b = 2

a*b = 8

a/b = 2

Remainder when a divided by b=0

The value of a after increment by 1 = 5

The value of b after decrement by 1 = 1

a+b = 6

a-b = 2

a*b = 8

a/b = 2

Remainder when a divided by b=0

The value of a after increment by 1 = 5

The value of b after decrement by 1 = 1

A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0.

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

== | Checks if the values of two operands are equal or not. If yes, then the condition becomes true. | (A == B) is not true. |

!= | Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true. | (A != B) is true. |

> | Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true. | (A > B) is not true. |

< | Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true. | (A < B) is true. |

>= | Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true. | (A >= B) is not true. |

<= | Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true. | (A <= B) is true. |

// Working of relational operators #include <stdio.h> int main() { int a = 5, b = 5, c = 10; printf("%d == %d is %d \n", a, b, a == b); printf("%d == %d is %d \n", a, c, a == c); printf("%d > %d is %d \n", a, b, a > b); printf("%d > %d is %d \n", a, c, a > c); printf("%d < %d is %d \n", a, b, a < b); printf("%d < %d is %d \n", a, c, a < c); printf("%d != %d is %d \n", a, b, a != b); printf("%d != %d is %d \n", a, c, a != c); printf("%d >= %d is %d \n", a, b, a >= b); printf("%d >= %d is %d \n", a, c, a >= c); printf("%d <= %d is %d \n", a, b, a <= b); printf("%d <= %d is %d \n", a, c, a <= c); return 0; }

Output :

5 == 5 is 1

5 == 10 is 0

5 > 5 is 0

5 > 10 is 0

5 < 5 is 0

5 < 10 is 1

5 != 5 is 0

5 != 10 is 1

5 >= 5 is 1

5 >= 10 is 0

5 <= 5 is 1

5 <= 10 is 1

5 == 5 is 1

5 == 10 is 0

5 > 5 is 0

5 > 10 is 0

5 < 5 is 0

5 < 10 is 1

5 != 5 is 0

5 != 10 is 1

5 >= 5 is 1

5 >= 10 is 0

5 <= 5 is 1

5 <= 10 is 1

An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming.

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

&& | Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. | (A && B) is false. |

|| | Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true. | (A || B) is true. |

! | Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false. | !(A && B) is true. |

// Working of logical operators #include <stdio.h> int main() { int a = 5, b = 5, c = 10, result; result = (a == b) && (c > b); printf("(a == b) && (c > b) is %d \n", result); result = (a == b) && (c < b); printf("(a == b) && (c < b) is %d \n", result); result = (a == b) || (c < b); printf("(a == b) || (c < b) is %d \n", result); result = (a != b) || (c < b); printf("(a != b) || (c < b) is %d \n", result); result = !(a != b); printf("!(a == b) is %d \n", result); result = !(a == b); printf("!(a == b) is %d \n", result); return 0; }

Output :

(a == b) && (c > b) is 1

(a == b) && (c < b) is 0

(a == b) || (c < b) is 1

(a != b) || (c < b) is 0

!(a != b) is 1

!(a == b) is 0

(a == b) && (c > b) is 1

(a == b) && (c < b) is 0

(a == b) || (c < b) is 1

(a != b) || (c < b) is 0

!(a != b) is 1

!(a == b) is 0

During computation, mathematical operations like: addition, subtraction, multiplication, division, etc are converted to bit-level which makes processing faster and saves power.

Bitwise operators are used in C programming to perform bit-level operations.

Operators | Meaning of operators |
---|---|

& | Bitwise AND |

| | Bitwise OR |

^ | Bitwise exclusive OR |

~ | Bitwise complement |

<< | Shift left |

>> | Shift right |

The following table lists the assignment operators supported by the C language −

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

= | Simple assignment operator. Assigns values from right side operands to left side operand | C = A + B will assign the value of A + B to C |

+= | Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand. | C += A is equivalent to C = C + A |

-= | Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand. | C -= A is equivalent to C = C - A |

*= | Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand. | C *= A is equivalent to C = C * A |

/= | Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand. | C /= A is equivalent to C = C / A |

%= | Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand. | C %= A is equivalent to C = C % A |

<<= | Left shift AND assignment operator. | C <<= 2 is same as C = C << 2 |

>>= | Right shift AND assignment operator. | C >>= 2 is same as C = C >> 2 |

&= | Bitwise AND assignment operator. | C &= 2 is same as C = C & 2 |

^= | Bitwise exclusive OR and assignment operator. | C ^= 2 is same as C = C ^ 2 |

|= | Bitwise inclusive OR and assignment operator. | C |= 2 is same as C = C | 2 |

Previous Next