Module Index |

► Arithmetic Operators |

► Logical Operators |

► Relational Operators |

► Bitwise Operators |

► Special Operators |

► Operator Precedence and Associativity |

► Postfix and Prefix Operators |

Operators are used to perform mathematical & logical operations. Operators supported by **C** can be categorized into
arithmetic, logical, relational, bitwise (used for bit manipulation) & some special operators. Let's see how these
operators are used in programming.

**arithmetic Operators**

**Arithmetic Operators** supported by **C** are **ADD ( + ), SUBTRACT ( - ), MULTIPLY ( * ), DIVIDE ( / ), MODULO ( % ), INCREMENT ( ++ ) and
DECREMENT ( -- )**.

The behaviour of

Let's look at the following program to see arithmetic operators in action :

*/*
* Demonstration of airthmetic operators
*/*
**#include <stdio.h>**
**int** main() {
**int** a = 5, b = 2;
**int** x, y, z, r, s, u, v;
x = a + b; */* addition ( Value of a+b is assigned to x ) */*
y = a - b; */* subtraction */*
z = a * b; */* multiplication */*
r = a / b; */* division */*
s = a % b; */* modulo */*
printf(" Sum of %d and %d is %d\n", a, b, x);
printf(" Difference of %d and %d is %d\n", a, b, y);
printf(" Product of %d and %d is %d\n", a, b, z);
printf(" Division of %d by %d is %d\n", a, b, r);
printf(" Modulo of %d by %d is %d\n", a, b, s);
printf(" Incrementing a : %d\n", ++a);
printf(" Decrementing b : %d\n", --b);
**return** 0;
}

Please note that some of the expressions in which a variable is present in both sides can be written as :

Expression | is Equivalent to |

X = X + Y | X += Y |

X = X - Y | X -= Y |

X = X * Y | X *= Y |

X = X / Y | X /= Y |

X = X % Y | X %= Y |

**Logical Operators**

**Logical Operators** supported by **C** are **AND ( && )**, **OR ( || )** and **NOT ( ! )**.

Consider two operands **A** and **B**.

**A && B** evaluates to **True** only if both **A** and **B** are **True**.

**A || B** evaluates to **True** if any of the two operands is **True**.

**' ! '** operator negates the state of an operand. For e.g, if **A = True**, then **!A = False**.

Note that any **non-zero** value is considered **True** and **zero** is considered **False**.

Following program shows logical operators in action :

*/*
* Demonstration of logical operator
*/*
**#include <stdio.h>**
**int** main() {
**int** a = 1, b = 0;
**int** x, y, z;
x = a && b; */* x = 0 ( false ) */*
y = a || b; */* y = 1 ( true ) */*
z = !b; */* z = 1 ( true ) */*
printf("%d AND %d = %d\n", a, b, x);
printf("%d OR %d = %d\n", a, b, y);
printf("NOT %d = %d", b, z);
**return** 0;
}

**Relational Operators**

**Relational operators** ( used for comparison operations ) supported by **C** are **EQUAL ( == ), NOT EQUAL ( != ), GREATER THAN ( > ),
LESS THAN ( < ), GREATER THAN OR EQUAL TO ( >= ) and LESS THAN OR EQUAL TO ( <= )**.

Following program illustrates the behaviour of relational operators :

*/*
* Demonstration of Relational Operators
*/*
**#include <stdio.h>**
**int** main() {
**int** a = 5, b = 2, c = 2;
**int** x, y, z, r;
x = ( b == c ); */* x = 1 (true) since b is equal to c */*
y = ( a != b ); */* y = 1 (true) since a is not equal to b */*
z = ( b < c ); */* z = 0 (false) since b is not less than c */*
r = ( b <= c ); */* r = 1 (true) since b is not less but equal to c */*
printf("x : %d\n", x);
printf("y : %d\n", y);
printf("z : %d\n", z);
printf("r : %d\n", r);
**return** 0;
}

**Bitwise Operators**

**Bitwise Operators** ( used for bit manipulation ) supported by **C** are **BITWISE AND ( & ), BITWISE OR ( | ), BITWISE XOR ( ^ ), BITWISE NOT ( ~ ), LEFT SHIFT ( << ) and RIGHT SHIFT ( >> )**. These operators operate at **bit** level.

Consider two bits **bit1** and **bit2**.

**bit1 & bit2** evaluates to **True** if and only if both **bit1** and **bit2** are **True**.

**bit1 | bit2** evaluates to **True** if any of the two bits is **True**.

**bit1 ^ bit2** evaluates to **True** if and only if one of the bits is **True**.

**~** operator toggles a bit. If **bit1 = True**, the **~bit1 = False.**

' **A << 2** ' left shifts the bits of a number (A) by 2 positions.

' **A >> 1** ' right shifts the bits of a number (A) by 1 position.

Suppose **A = 7** and **B = 19**. In binary format : **A = 00000111** and **B = 00010011**

Following table shows how bitwise operators do bit manipulation :

Binary |
||||||||

A | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 |

B | 0 | 0 | 0 | 1 | 0 | 0 | 1 | 1 |

A & B | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 |

A | B | 0 | 0 | 0 | 1 | 0 | 1 | 1 | 1 |

A ^ B | 0 | 0 | 0 | 1 | 0 | 1 | 0 | 0 |

~A | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 0 |

A << 2 | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 0 |

A >> 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 |

Following program demonstrate the behaviour of bitwise operators :

*/*
* Demonstration of bitwise operators
*/*
**#include <stdio.h>**
**int** main() {
**int** a = 7, b = 19;
**int** x, y, z, r, s;
x = a & b; */* 7 & 19 = (00000111 & 00010011) = 00000011 = 3 */*
y = a | b; */* 7 | 19 = (00000111 | 00010011) = 00010111 = 23 */*
z = a ^ b; */* 7 ^ 19 = (00000111 ^ 00010011) = 00010100 = 20 */*
r = a << 2; */* 7 << 2 = (00000111 << 2) = 00011100 = 28 */*
s = a >> 1; */* 7 >> 1 = (00000111 >> 1) = 00000011 = 3 */*
printf("%d & %d = %d\n", a, b, x);
printf("%d | %d = %d\n", a, b, y);
printf("%d ^ %d = %d\n", a, b, z);
printf("%d << 2 = %d\n", a, r);
printf("%d >> 1 = %d\n", a, s);
**return** 0;
}

Note that in the above program, there is no demonstration of **NOT ( ~ )** operator since the result would be unexpected and will confuse you. You might have noticed that all decimal numbers are represented using 8 bits

( 1 Octet ) but in the system memory it is stored as a sequence of multiple octets (4 in case of 32 bit systems). In our case, other 3 octets would be all **0**s. But **NOT** operation would toggle all those **0**s to **1**s which would result into a very high value ( outside the range of **int** ). As a result, some unexpected value would be displayed.

**Special Operators**

There are some special characters used for specific purpose in **C** :

sizeof | It is used to get the size of a data type i.e the memory space required by a variable of a particular data type. |

Condition | Conditional Operator returns a value depending on a condition. Syntax : ( Condition ? a : b ). If condition is true, a is returned else b
is returned. |

Assignment | It is used to assign one value to other in an expression. In the expression c = a + b, value of ( a + b ) is assigned to c. |

Refer to the section on **C** Tokens and Keywords for sample program demonstrating the use of **sizeof** operator.
Following program illustrates the use of conditional operator :

*/*
* Demonstrating Conditional Operator
*/*
**#include <stdio.h>**
**int** main() {
**int** a = 5, b = 2;
*/* if a > b, then value in 'a' assigned to 'c'
else value in 'b' is assigned to 'a' */*
**int** c = ( a > b ) ? a : b; */* c holds greater of a and b */*
printf("Greater of %d and %d is %d", a, b, c);
**return** 0;
}

**Operator Precedence & Associativity**

**Precedence** of operators determines the order of evaluation of expression consisting of multiple operators.

Higher precedence operators will
be evaluated first. **Associativity** determines whether an expression is evaluated **left to right** or **right to left**. Consider the expressions below :

x = 2 + 3 * 2 => x = 2 + 6 => x = 8 ( '*' has higher precedence than '+' ) |

y = 2 * 3 / 2 => y = 6 / 2 => y = 3 ( '*' and '/' has same precedence but associativity is left to right ) |

z = 3 / 2 * 2 => z = 1 * 2 => z = 2 ( '*' and '/' has same precedence but associativity is left to right ) |

Following table shows the precedence and associativity of the **C** operators ( Operator precedence decreases as we move downwards ) :

Operators | Precedence |

Postfix ( ++, -- ) | Left to Right |

Unary ( +, -, !, ~, sizeof ), Prefix ( ++, -- ) | Right to Left |

*, /, % | Left to Right |

Additive ( +, - ) | Left to Right |

Left and Right Shift ( <<, >> ) | Left to Right |

Relational ( <, <=, >, >= ) | Left to Right |

Equality ( ==, != ) | Left to Right |

Bitwise AND ( & ) | Left to Right |

Bitwise XOR ( ^ ) | Left to Right |

Bitwise OR ( | ) | Left to Right |

Logical AND ( && ) | Left to Right |

Logical OR ( || ) | Left to Right |

Conditional ( Condition ? x : y ) | Right to Left |

Assignment ( = ) | Right to Left |

*Note : **Unary** operators have single operand

**Postfix and Prefix Operators**

The increment (++) and decrement (--) operators can appear either on left side (prefix) or right side (postfix) of the operand.
Following program demonstrates the behaviour of postfix and prefix operators :

*/*
* Demonstration of postfix and prefix operators
*/*
**#include <stdio.h>**
**int** main() {
**int** a = 5, b = 2;
**int** c = 7, d = 3;
**int** x, y, r, s;
x = ++a; */* first increment a, then assign its value to x */*
printf("x : %d , a : %d\n", x, a); */* displays x : 6, a : 6 */*
y = b++; */* first assign value of b to y, then increment b */*
printf("y : %d , b : %d\n", y, b); */* displays y : 2, b : 3 */*
r = --c; */* first decrement c, then assign its value to r */*
s = d--; */* first assign value of d to s, then decrement d */*
printf("r : %d , c : %d\n", r, c); */* displays r : 6, c : 6 */*
printf("s : %d , d : %d\n", s, d); */* displays s : 3, d : 2 */*
**return** 0;
}