# Operators in C

• Arithmetic operators

• Cast operator

• Increment and Decrement

• Bitwise operators

• Comparison operators

• Assignment operators

• sizeof operator

• Conditional expression operator

## Arithmetic Operators

C supports the arithmetic operators        :

-              Subtraction
*             Multiplication
/              Division
%            Modulo (remainder)

## The Cast Operator

The cast operator temporarily changes the type of a variable
if either operand is a double,

1
2
3
4
5
6
7
8
9
10
11
```intmain(void)
{
inti=s,j=4;
doublef;

f=(double)i/j;
f=i/(double)j;
f=(doub1e)i/(doub1e)j;
f=(doub1e)-(i/j);
return0;
}```

## Increment and Decrement Operator

c has two special operators for adding and subtracting one from a variable

++  iincrement
--    decrement

These may be either prefix (before the variable) or postfix (after the variable):

Example :

int i =5, j = 4;

i++;                        /*“i’ becomes 6 */

j++;                        /*“j”becomes3 */

--i;                          /*”1” becomes 7 */

## Prefix and Postfix

• The prefix and postfix versions are different
1
2
3
4
5
6
7
8
9
10
11
```#include <stdio.h>
intmain(void)
{
inti,j=5;
i=++j;
printf(“i=%d,j=%d\n”,i,j);
j=5;
i=j++;
printf(“i=%d,j=%d\n”,i,j);
return0;
}```

here first is equivalent to :

• j++;
• i=j;

second is equivalent to :

• i=j;
• j++;

Result is like

i=6 and j=6;

i=5 and j=6;

## Comparison Operators

•  C supports the comparison operators:
<             less than
<=           less than or equal to
>             greater than
>=           greater than or equal to
==           is equal to
!=            is not equal to
• These all give 1 (non zero value, i.e. true) when the comparison succeeds and 0 (i.e. false) when the comparison fails

## Logical Operators

• C supports the logical operators:

&&         and
||           or
!             not

• These also give I (non zero value, i.e. true) when the condition succeeds and 0 (i.e. false) when the condition fails
1
2
```inti,j=10,k=29;
i=((j>5)&&(k<100))||(k>24);```

## Bitwise Operators

C has the following bit operators which may only be applied to integer types:

&             bitwise  and
|              bitwise inclusive or
^             bitwise exclusive or
~             one’s compliment
>>           right shift
<<           left shift

## Assignment

Assignment is more flexible than might first appear
An assigned value is always made available for subsequent use

1
2
3
```int i, j, k, l, m, n;
i = j = k = l = m = n;
printf("%i\n, j = 93");```

“n= 22” happens first, this makes 22 available for assignment to “m”. Assigning 22 to “m” makes 22 available for assignment to “I” etc.

“j” is assigned 93, the 93 is then made available to printf for printing.

## Other Assignment Operators

There is a family of assignment operators:

+=           -=            *=           /=           %=

&=          |=           ^=

<<=        >>=

In each of these:
expression1 op= expression2

is equivalent to:
(expression1) = (expression1) op (expression2 )

1
2
3
4
5
6
7
8
9
```a += 27;
a = a + 27;

f /= 9.2;
f = f / 9.2;

i *= (j + 2);
i = i * (j + 2);
is equivalent to: ```