C : Operators
Tags: cpp, programming, operations
Published: 2021-03-15 15:13:42

1. What are operators and Types?
2. Categories with example

****
1.**What are operators and Types?**
 -An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. For example + is an operator to perform addition.
 -Categories of operators depends upon no. of operands.

2.**Arithmetic Operator:**performs mathematical operation on numerical values (constants and variables).
**2.1  +	:** addition or unary plus
**2.2  -	:** subtraction or unary minus
**2.3  *	:** multiplication
**2.4  /	:** division
**2.5  %	:** remainder after division (modulo division)
**2.6 ++        :**increment (pre and post increment)
**2.7 --        :**decrement (pre and post decrement)

```
/*arithmetic operators*/
#include <stdio.h>
int main()
{
    int a = 5,b = 7, 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/b = %d \n",c);
    printf("++a = %d \n", ++a);
    printf("--b = %d \n", --b);
    printf("++c = %f \n", ++c);
    printf("--d = %f \n", --d);
    
    return 0;
}
```
Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. These two operators are unary operators, meaning they only operate on a single operand.

3.**Relational Operator:** checks the relationship between two operands. Returns 1, if the relation is true,else returns value 0. They are used in decision making and loops.
**3.1  ==	:** Equal to
**3.2  !=	:** Not Equal to
**3.3  <	:** Less than
**3.4  >	:** Greater than
**3.5  <=	:** Less than or equal to
**3.6  >=	:** Greater than or equal to

```
// relational operators
#include <stdio.h>
int main()
{
    int a = 9, 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;
}
```
4.**Assignment Operator:** used for assigning a value to a variable.
**4.1  =	:** similar to a = b
**4.2  +=	:** similar to a=a+b
**4.3  -=	:** similar to a=a-b
**4.4  *=	:** similar to a=a*b
**4.5  /=	:** similar to a=a/b
**4.6  %=	:** similar to a=a%b

```
// assignment operators
#include <stdio.h>
int main()
{
    int a = 8, c;

    c = a;      // c is 5
    printf("c = %d\n", c);
    c += a;     // c is 10 
    printf("c = %d\n", c);
    c -= a;     // c is 5
    printf("c = %d\n", c);
    c *= a;     // c is 25
    printf("c = %d\n", c);
    c /= a;     // c is 5
    printf("c = %d\n", c);
    c %= a;     // c = 0
    printf("c = %d\n", c);

    return 0;
}
```

5.**Logical Operator:** expression with 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.
**5.1  &&	:** Logical AND. True only if all operands are true	
**5.2  ||	:** Logical OR. True only if either one operand is true	
**5.3  !	:** Logical NOT. True only if the operand is 0	

```
// logical operators
#include <stdio.h>
int main()
{
    int a = 9, b = 2, c = 16, 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;
}
```
6.**Bitwise Operator:** mathematical operations are converted to bit-level during computation which makes processing faster and saves power. They are are used in C programming to perform bit-level operations.
**6.1  &	:** Bitwise AND
**6.2  |	:** Bitwise OR
**6.3  ^	:** Bitwise exclusive OR
**6.4  ~	:** Bitwise complement
**6.5  <<	:** Shift left
**6.6  >>	:** Shift right

```
// bitwise operators 
#include <stdio.h> 
int main() 
{ 
    unsigned char a = 5, b = 9; 
    printf("a = %d, b = %d\n", a, b); 
    printf("a&b = %d\n", a & b); 
    printf("a|b = %d\n", a | b); 
    printf("a^b = %d\n", a ^ b); 
    printf("~a = %d\n", a = ~a); 
    printf("b<<1 = %d\n", b << 1); 
    printf("b>>1 = %d\n", b >> 1); 
  
    return 0; 
} 
```

7.**Misc Operator:** 
**7.1  sizeof()	:** Returns the size of a variable.	
**7.2  &	:** Returns the address of a variable.
**7.3  *	:** Pointer to a variable.
**7.4  ?	:** Conditional Expression.

8.**Ternary Operator:** ternary operator evaluates the test condition and executes a block of code based on the result of the condition.
**condition ? expression1 : expression2;** 
-Here, condition is evaluated and if condition is true, expression1 is executed else if condition is false, expression2 is executed.
-The ternary operator takes 3 operands (condition, expression1 and expression2). Hence, the name ternary operator.
```
//without ternary operator
int a = 1, b = 2, ans;
if (a == 1) {
    if (b == 2) {
        ans = 3;
    } else {
        ans = 5;
    }
} else {
    ans = 0;
}
printf ("%d\n", ans);
```
```
//with ternary operator i.e. conversion of above code
int a = 1, b = 2, ans;
ans = (a == 1 ? (b == 2 ? 3 : 5) : 0);
printf ("%d\n", ans);
```