# Operators

### What is operator?

An operator is a symbol used to perform certain mathematical or logical operations. It is used in programs to manipulate data and variables.

##### Types of Operators in C
• Arithmetic operators
• Relational operators
• Logical operators
• Assignment operators
• Increment and decrement operators
• Conditional operators
• Bitwise operators
• Special operators

##### Arithmetic Operators If a & b are two integer variable are called Operands.
Examples: a-b, a+b, a*b, a/b, a%b

• if a & b are integer than after Arithmetic operation ,the result is also integer.
• if a & b are float or double than after Arithmetic operation ,the result is also float or double.
• The % modulo operator cannot used on floating point data.

##### Relational Operators

Relational Operators are used to compare and check the relationship between two operands.
Example: comparing the age of two persons( 10<20 is true, 20<10 is false, 20!=20 is false ). ##### Logical Operators

C has the following three logical operators. EXAMPLES: if (11>10 && 15>16) is false, if (11>10 || 15>16) is true.

##### Assignment Operators

Assignment operators are used to assign the result of an expression to a variable (Right operands to its left operands).
Equal (‘=’) Operator is used as assignment operator.
Example: x=5+4;
The result of expression “5+4” i.e 9 will be assigned to variable x . ``` #include<stdio.h> int main() { int a,n; printf("Enter the value of a "); scanf("%d",&a); printf("Enter the value of n "); scanf("%d",&n); while(a<n) { printf("%d\n",a); a*=a; }; return 0; } ```

#### Output:

Enter the value of a 2
Enter the value of n 100
2
416

##### Conditional Operators

Conditional operators (? :) are ternary operators that takes three expressions as operand as shown below:
Syntax: exp1 ? exp2 : exp3
It evaluates exp2 when exp1 is True otherwise exp3 is evaluated .

``` #include<stdio.h> int main() { nt a,b,c,d; a=15; b=10; c=a++-b; printf("a = %d b = %d c = %d\n",a,b,c); d=b++ + a; printf("a/b = %d\n",a/b); printf("a%%b = %d\n",a%b); printf("a *=b = %d\n",a*=b); //assignment operator printf("%d\n", (c>d) ? 1:0); // Conditional operator printf("%d\n", (c<d) ? 1:0); // Conditional operator printf("%d\n",(a<d) ? 1:0); // Conditional operator return 0; } ```

#### Output:

a = 16 b = 10 c = 5
a/b = 1
a%b = 5
a *=b = 176
0
1
0

##### Increment and Decrement operators

Increment & Decrement are Unary Operators that add or subtract, to or from its operand.
Examples: ++a, a++, a--, --a

``` #include<stdio.h> int main() { a=5; y=++a; printf(“y=%d a=%d”,y,a); } ```

#### Output:

y=6 a=6

``` #include<stdio.h> int main() { a=5; y=a++; printf(“y=%d a=%d”,y,a); } ```

#### Output:

y=5 a=6

``` #include<stdio.h> int main() { a=5; y=--a; printf(“y=%d a=%d”,y,a); } ```

#### Output:

y=4 a=4

``` #include<stdio.h> int main() { a=5; y=a--; printf(“y=%d a=%d”,y,a); } ```

#### Output:

y=5 a=4

##### Bitwise Operators

For manipulation of data at bit level a special type of operators are used called Bitwise Operators.
We used it for shifting the bits right or left.
Note:Bitwise operator may not be applied to float or double. It takes only Integer as operand.

``` #include<stdio.h> int main() { int x,y,y1; x=122; y=x>>1; //right shift of bit by one printf("x is %d\n",x); printf("y is %d\n",y); x=x>>1; printf("x is %d\n",x); x=122; y1=x<<1; //left shift of bit by one printf("x is %d\n",x); printf("y1 is %d\n",y1); return 0; } ```

#### Output:

x is 122
y is 61
x is 61
x is 122
y1 is 244

##### Special Operators

C support some special type of operators such as comma operator , sizeof operator , pointer operator (*) .
Example:
< m=sizeof (sum); // sizeof operator
int a,b; //comma operator

#### Precedence of operators

Precedence determines the order in which operators are evaluated in the given expression in absense of paraentheses.
Note: Paraentheses is used to override the operator precedence.
x=9-4+3*2-1
Here we use precedence operator rule as priority is given in table.

##### Precedence Table

C operators are listed in order of precedence (highest to lowest). Their associativity indicates in what order operators of equal precedence in an expression are applied . Key points
• When ++ or -- is used as postfix like y=a++.
Then first, the value of ‘a’ is assign to y, after that ‘a’ is incremented.
• When ++ or -- is used as prefix like y= ++a.
Then first,the value of ‘a’ is incremented after that the resultant value of ‘a’ is assigned to y.

Do You Know?