# Operators in C

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 operators:

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

Assignment operators:

Assignment operators supported by C language are as follows:

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.

Truth Table:

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.

Relational operator

The following table shows the relational operator supported by C

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:

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