# Operators in C Programming

A symbol which specifies the mathematical, logical or relational operations for performing actions is defined as an operation.

Operators are categorized in the following groups:

1. Arithmetic operators
2. Relational operators
3. Equality operators
4. Logical operators
5. Unary operators
6. Conditional operators
7. Bitwise operators
8. Assignment operators
9. Comma operator
10. Sizeof operator

## 1. Arithmetic operators

C supports five arithmetic operators which are as follows:

OperatorSign
Subtraction-
Multiplication*
Division/
Modulus%

The operands must contain only the numeric values like integer, float or character quantities. If both the operands are divided then the result will also be an integer.
If both the operands or either of it are floating point then the result will be a floating point.

Example: Demonstrate arithmetic operator
float pi=3.14;
float area;
area=pi*r*r; //Multiplication arithmetic operator.

## 2. Relational Operator

A relational operator is also known as a Comparison operator. It compares two values. They return the true or false value depending on the conditional relationship between the two operands.

The relational operators are as follows:

Operator Sign
Less than<
Greater than>
Greater than equal to>=
Less than equal to<=

These operators are evaluated from left to right.

## 3. Equality operator

There are two types of equality operators that are supported by C.

They are as follows:

OperatorMeaning
==It returns 1 if both the operands are equal or then it will return a 0.
!=If the operands do not have the same value it will return 1 else 0.

## 4. Logical Operators

C supports three types of operators which are as follows:

OperatorDescriptionExample
Logical AND (&&)It is used to evaluate two conditions with the relational operators. If the left and right expression is true then the whole expression is true.(a<b) && (a>c)
Logical OR (| |)It is used to evaluate simultaneously. If one or both the sides of the expression is true then the whole expression is true.(a<b) | | (a>c)
Logical NOT  (!)It takes a single expression and negates the value. It produces a zero if the expression gives a non-zero value and it will give a value 1 if the expression produces a zero. It reverses the value of the expression.b=!a

Truth table for all the operators:

XYX&&YX| |Y
0000
0101
1001
1111

## 5. Unary operators

These operators act on the single operands. There are three types of operators
minus, increment and decrement.

i) Minus operator
A minus sign is preceded to a numerical constant, variable or expression. Used for negation. It has a different meaning than the arithmetic subtraction.

Example:
-3  -5.0

ii) Increment and decrement operator
The increment operator increases the value of the operand by 1. Similarly, the decrement operator decreases the value of the operand by 1.
They have two variants: prefix and postfix.

Prefix: Here, the expression is applied before the operator.

Example: Demonstrating prefix
a=++b;
Here, the value of b is returned before incrementing.

Postfix: Here, the expression is applied after the operator.

Example: Demonstrating postfix
a=b++;
Here, the value of b is incremented and then returned.

## 6. Conditional operator

They are also called as Ternary operator (? :). This operator is just like the if-else statement which can be within the expression. It can be used where there are more than two or more alternatives given for an expression.

Syntax:
exp1 ? exp2 : exp3

In the above syntax, exp1 will be evaluated first. If exp1 is true then exp2 is evaluated and it becomes the result of the expression else exp3 is evaluated and it becomes the result of the expression.

Example: Demonstrating conditional operator
small = (x > y) ? a : b

## 7. Bitwise Operators

These operators work on the bits. They perform bit-by-bit operations.

In all there are six bitwise operators, as follows:

OperatorSymbolDescription
Bitwise AND&It is a small version of boolean AND. It performs operations on bits instead of bytes, chars etc.
Bitwise OR|It is a small version of boolean OR. It performs operations on bits instead of bytes, chars etc.
Bitwise XOR^It performs operations on the individual bits of the operands.
Bitwise NOT~It performs the logical negation on each bit of the operand. By performing negation it produces the 1s complement of the given binary value.
Left shift<<Here the bits are shifted to the left by one place.
Right shift>>Here the bits are shifted to the right by one place.

Truth table for AND, OR and XOR is as follows:

XYX & YX | YX ^ Y
00000
01011
10011
11110

## 8. Assignment operators

They are responsible for assigning values to the variables. Equal to sign (=) is the most common assignment operator. Whenever an equal to sign is encountered in an expression the compiler will process the statement on the right side of the sign and will assign the result to the variable on left side.

Example: Demonstrate assignment operators
c = a+b;

## 9. Comma operator

This operator takes two operands. It separates two values, conditions etc. They are evaluated from left to right sequence. The comma operator has the lowest prcedence.

Example:
int a,b,c;

## 10.  Sizeof operator

This operator is used for calculating the size of the data-types. They can be applied to all the data-types. For using this operator the 'sizeof' keyword is used followed by the type name, variable or expression. It determines the amount of memory space that the variable/data/expression will take.

## Precedence and Associativity

OperatorAssociativity
() [ ] . → ++ --left to right
++ -- + - ! ~ (type) * & sizeofright to left
* / %left to right
+ -left to right
<<  >>left to right
< <= > >=left to right
== !=left to right
&left to right
^left to right
|left to right
&&left to right
||left to right
?:left to right
=  +=  -+  *=  /=  %=  &=  ^=  |=  <<=  >>=right to left
,left to right