Chapter 5: Operators in C

1. Arithmetic Operators

These are used for mathematical calculations.

Operator Name Example Result (if a=10, b=3)
+ Addition a + b 13
Subtraction a – b 7
* Multiplication a * b 30
/ Division a / b 3 (integer division)
% Modulus (remainder) a % b 1

Important Notes:

  • / between two integers gives integer division (removes decimal part)
  • Use float or double if you want decimal result

Example:

C

Output:

text

2. Relational Operators

These compare two values and return true (1) or false (0).

Operator Meaning Example Result (a=10, b=3)
== Equal to a == b 0 (false)
!= Not equal to a != b 1 (true)
> Greater than a > b 1
< Less than a < b 0
>= Greater than or equal a >= b 1
<= Less than or equal a <= b 0

Example:

C

3. Logical Operators

Used to combine conditions (AND, OR, NOT).

Operator Name Meaning Example
&& AND True if both are true (a > 5 && b < 10)
OR
! NOT Reverses the value (true → false) !(a == b)

Example:

C

4. Assignment Operators

Used to assign values to variables.

Operator Example Same as
= x = 10 x = 10
+= x += 5 x = x + 5
-= x -= 3 x = x – 3
*= x *= 2 x = x * 2
/= x /= 4 x = x / 4
%= x %= 3 x = x % 3

Example:

C

5. Increment / Decrement Operators

Operator Name Example Effect
++ Increment x++ x = x + 1 (postfix)
++ Pre-increment ++x x = x + 1 (before use)
Decrement x– x = x – 1 (postfix)
Pre-decrement –x x = x – 1 (before use)

Example:

C

6. Bitwise Operators

Work on bits (0 and 1) – used in low-level programming.

Operator Name Example (a=5 → 0101, b=3 → 0011) Result
& AND a & b 1
OR `a
^ XOR a ^ b 6
~ NOT (ones complement) ~a -6
<< Left shift a << 1 10
>> Right shift a >> 1 2

Example (simple):

C

7. Precedence and Associativity

Operators have priority order (like BODMAS).

Common Precedence Order (Highest to Lowest):

  1. ++ — (postfix), ()
  2. ++ — (prefix), !, ~
  3. * / %
  4. + –
  5. << >>
  6. < <= > >=
  7. == !=
  8. &
  9. ^
  10. &&
  11. ||
  12. =+=-= etc.

Associativity:

  • Most operators are left to right
  • Assignment operators are right to left

Example:

C

Today’s Homework

  1. Write a program that takes two numbers from user and performs all arithmetic operations (sum, diff, product, div, mod) and prints them.
  2. Create a program that checks if a number is even or odd using % operator.
  3. Write a program using logical operators to check if a person can vote (age >= 18 AND citizen == ‘Y’).
  4. Experiment with increment/decrement: print values of x++, ++x, x–, –x in different lines.

You may also like...