# Operators In C

## Operators In C

An operator is a symbol that is used for performing certain operations like arithmetical, logical, relational, assignment, etc.

### Types of Operators

• Arithmetic Operators
• Relational Operators
• Assignment Operators
• Bitwise Operators
• Ternary or Conditional Operators
• Logical Operator
• Misc Operator

Let's look each one by one with example

### Arithmetic Operators

Code

``#include<stdio.h>int main(){  int a = 2,b = 2, r;  r = a+b;  printf("Addition: a+b = %d \n",r);  r = a-b;  printf("Subtraction: a-b = %d \n",r);  r = a*b;  printf("Multiplication: a*b = %d \n",r);  r = a/b;  printf("Division: a/b = %d \n",r);  r = a%b;  printf("Remainder: %d \n",r);  r = ++a;  printf("Increment the value of a by 1: %d \n",r);  r = --b;  printf("Decremented the value of b by 1: %d \n",r);  return 0;}``

Output

``Addition: a+b = 4 Subtraction: a-b = 0 Multiplication: a*b = 4 Division: a/b = 1 Remainder: 0 Increment the value of a by 1: 3 Decremented the value of b by 1: 1 ``

### Relational Operators

Relational operator checks the relation between two operands, and return 1(true) or 0(false) based on the operator.

Code

``#include <stdio.h>int main(){  int a = 2, b = 2;  printf("%d == %d is %d \n", a, b, a == b);  printf("%d > %d is %d \n", a, b, a > b);  printf("%d < %d is %d \n", a, b, a < b);  printf("%d != %d is %d \n", a, b, a != b);  printf("%d >= %d is %d \n", a, b, a >= b);  printf("%d <= %d is %d \n", a, b, a <= b);  return 0;}``

Output

``2 == 2 is 1 2 > 2 is 0 2 < 2 is 0 2 != 2 is 0 2 >= 2 is 1 2 <= 2 is 1 ``

### Assignment Operators

Assignement operator assign value to a variable.

Code

``#include <stdio.h>int main(){  int a = 2, result;  result = a;  printf("Value of result = %d\n", result);  result += a;  printf("Value of result = %d\n", result);   result -= a;   printf("Value of result = %d\n", result);  result *= a;    printf("Value of result = %d\n", result);  result /= a;  printf("Value of result = %d\n", result);  return 0;}``

Output

``Value of result = 2Value of result = 4Value of result = 2Value of result = 4Value of result = 2``

### Bitwise Operators

In C language we get operators for bit operation between two variables.

Code

``#include <stdio.h>int main() {int a = 2, b = 3, result = 0;         result = a & b;     printf("AND Operator of a and b is %d\n", result );result = a ^ b;printf("XOR Operator of a and b is %d\n", result );result = ~a;printf("Ones Complement Operator of a is %d\n", result );result = a << 2;printf("Left Shift Operator of a is %d\n", result );result = a >> 2;printf("Right Shift Operator of a is %d\n", result );return 0;}``

Output

``AND Operator of a and b is 2XOR Operator of a and b is 1Ones Complement Operator of a is -3Left Shift Operator of a is 8Right Shift Operator of a is 0``

### Ternary or Conditional Operators

It is used in decision making statements.

expression? statement1: statement2;

It is a boolean expression, it can be either true or false.
If the expression returns true then statement1 will get executed.
If the expression returns false then statement2 will get executed.

Code

``#include <stdio.h>  int main()  {  int num=2;(num>1)? (printf("It is greater than number 1!")) : (printf("It is less than number 1!"));return 0;  }``

Output

``It is greater than number 1!``

### Logical Operators

It is used in decision making. It returns 0(false) or 1(true).

Code

``#include <stdio.h>    int main ()  {      int n = 2;      printf (" %d \n", (n == 2 && n >= 1));    printf (" %d \n", (n >= 3 && n >= 0));      printf (" %d \n", (n == 2 && n >= 2));      printf (" %d \n", (n >= 1 && n <= 2));            return 0;  } ``

Output

`` 1  0  1 1``

### Misc Operators

It includes operators like sizeof(), reference operator(&), pointer operator(*), etc

Code

``#include<stdio.h>int main(){int num = 10, *pointer;pointer = &num;printf("Memory address: %d\n",pointer);return 0;}``

Output

``Memory address: 6422216``