Module 3: Operators in C
Learning Objectives
By the end of this module, you will be able to:
- Understand what operators are and how they work.
- Perform arithmetic calculations in C.
- Compare values using relational operators.
- Combine conditions with logical operators.
- Assign values using assignment operators.
- Use increment and decrement operators effectively.
- Perform bit-level operations using bitwise operators.
- Make decisions using the ternary operator.
- Understand operator precedence and associativity.
- Build simple programs using different operators.
Introduction
Operators are one of the most important building blocks of any programming language. They allow us to perform calculations, compare values, manipulate data, and make decisions.
For example:
- Add two numbers
- Check whether a student passed
- Find the largest of two numbers
- Test if a number is even or odd
- Perform binary operations for efficient programming
In C, operators work on operands (variables or values) to produce a result.
Example:
1int sum = 10 + 5;
Here:
10and5are operands.+is the operator.15is the result.
Types of Operators in C
C provides several categories of operators:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
- Increment and Decrement Operators
- Bitwise Operators
- Ternary Operator
- Other Operators (covered in later modules)
Arithmetic Operators
Arithmetic operators perform mathematical calculations.
| Operator | Description | Example |
|---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (Remainder) | a % b |
Addition
1#include <stdio.h> 2 3int main() 4{ 5 int a = 20; 6 int b = 10; 7 8 printf("%d", a + b); 9 10 return 0; 11}
Output
130
Subtraction
1printf("%d", 20 - 5);
Output
115
Multiplication
1printf("%d", 6 * 5);
Output
130
Division
1printf("%d", 20 / 5);
Output
14
Integer Division
1printf("%d", 5 / 2);
Output
12
The decimal part is discarded because both operands are integers.
Floating-Point Division
1printf("%.2f", 5.0 / 2);
Output
12.50
Modulus Operator
Returns the remainder after division.
1printf("%d", 10 % 3);
Output
11
Common uses:
- Even/Odd checking
- Leap year calculation
- Circular indexing
Relational Operators
Relational operators compare two values.
The result is:
1→ True0→ False
| Operator | Meaning |
|---|---|
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
Example:
1#include <stdio.h> 2 3int main() 4{ 5 int a = 10; 6 int b = 20; 7 8 printf("%d\n", a < b); 9 printf("%d\n", a == b); 10 11 return 0; 12}
Output
11 20
Logical Operators
Logical operators combine multiple conditions.
| Operator | Meaning | ||
|---|---|---|---|
&& | Logical AND | ||
| ` | ` | Logical OR | |
! | Logical NOT |
Logical AND (&&)
Returns true only if both conditions are true.
1int age = 20; 2 3if(age >= 18 && age <= 60) 4{ 5 printf("Eligible"); 6}
Logical OR (||)
Returns true if at least one condition is true.
1int marks = 85; 2 3if(marks >= 90 || marks >= 80) 4{ 5 printf("Excellent"); 6}
Logical NOT (!)
Reverses the result of a condition.
1int isStudent = 0; 2 3if(!isStudent) 4{ 5 printf("Not a student"); 6}
Assignment Operators
Assignment operators assign values to variables.
| Operator | Example | Equivalent |
|---|---|---|
= | a = 5 | Assign value |
+= | a += 2 | a = a + 2 |
-= | a -= 2 | a = a - 2 |
*= | a *= 2 | a = a * 2 |
/= | a /= 2 | a = a / 2 |
%= | a %= 2 | a = a % 2 |
Example:
1int x = 10; 2 3x += 5; 4 5printf("%d", x);
Output
115
Increment and Decrement Operators
These operators increase or decrease a variable by one.
| Operator | Meaning |
|---|---|
++ | Increment |
-- | Decrement |
Pre-Increment
1int a = 5; 2 3printf("%d", ++a);
Output
16
The value is increased before it is used.
Post-Increment
1int a = 5; 2 3printf("%d", a++); 4printf("%d", a);
Output
15 26
The current value is used first, then incremented.
Pre-Decrement
1int a = 5; 2 3printf("%d", --a);
Output
14
Post-Decrement
1int a = 5; 2 3printf("%d", a--); 4printf("%d", a);
Output
15 24
Bitwise Operators
Bitwise operators work directly on the binary representation of integers.
| Operator | Description | |
|---|---|---|
& | Bitwise AND | |
| ` | ` | Bitwise OR |
^ | Bitwise XOR | |
~ | Bitwise NOT | |
<< | Left Shift | |
>> | Right Shift |
Bitwise AND
1int a = 6; 2int b = 3; 3 4printf("%d", a & b);
Binary:
16 = 110 23 = 011 3------------ 4 010
Output
12
Bitwise OR
1printf("%d", 6 | 3);
Output
17
Bitwise XOR
1printf("%d", 6 ^ 3);
Output
15
Bitwise NOT
1printf("%d", ~5);
The exact result depends on the system's integer representation, but on most modern systems using two's complement, the output is:
1-6
Left Shift
1printf("%d", 5 << 1);
Output
110
Equivalent to multiplying by 2 for positive integers.
Right Shift
1printf("%d", 8 >> 1);
Output
14
Equivalent to dividing by 2 for positive integers.
Ternary Operator
The ternary operator is a shorthand for if-else.
Syntax
1condition ? expression1 : expression2;
Example:
1#include <stdio.h> 2 3int main() 4{ 5 int age = 20; 6 7 printf("%s", age >= 18 ? "Adult" : "Minor"); 8 9 return 0; 10}
Output
1Adult
Operator Precedence
Operator precedence determines the order in which operators are evaluated.
Example:
1int result = 10 + 5 * 2;
Result:
120
Multiplication is performed before addition.
Common Operator Precedence
| Priority | Operators | ||
|---|---|---|---|
| Highest | () | ||
++ -- | |||
* / % | |||
+ - | |||
< <= > >= | |||
== != | |||
&& | |||
| ` | ` | ||
| Lowest | = |
Example
1int value = (10 + 5) * 2;
Output
130
Parentheses have the highest precedence.
Practice Project 1: Simple Calculator
1#include <stdio.h> 2 3int main() 4{ 5 float a, b; 6 7 printf("Enter two numbers: "); 8 scanf("%f %f", &a, &b); 9 10 printf("Addition = %.2f\n", a + b); 11 printf("Subtraction = %.2f\n", a - b); 12 printf("Multiplication = %.2f\n", a * b); 13 14 if (b != 0) 15 printf("Division = %.2f\n", a / b); 16 else 17 printf("Division by zero is not allowed.\n"); 18 19 return 0; 20}
Practice Project 2: Even/Odd Checker
1#include <stdio.h> 2 3int main() 4{ 5 int number; 6 7 printf("Enter a number: "); 8 scanf("%d", &number); 9 10 if(number % 2 == 0) 11 printf("Even Number"); 12 else 13 printf("Odd Number"); 14 15 return 0; 16}
Practice Project 3: Largest Number
1#include <stdio.h> 2 3int main() 4{ 5 int a, b; 6 7 printf("Enter two numbers: "); 8 scanf("%d %d", &a, &b); 9 10 if(a > b) 11 printf("%d is the largest.", a); 12 else if(b > a) 13 printf("%d is the largest.", b); 14 else 15 printf("Both numbers are equal."); 16 17 return 0; 18}
Common Mistakes to Avoid
- Using
=instead of==in comparisons. - Dividing by zero without checking the denominator.
- Assuming integer division returns decimal values.
- Misunderstanding the difference between pre-increment (
++a) and post-increment (a++). - Ignoring operator precedence; use parentheses to make expressions clear.
- Using bitwise operators (
&,|) when logical operators (&&,||) are intended.
Best Practices
- Use parentheses to improve readability in complex expressions.
- Validate user input before performing operations like division.
- Prefer descriptive variable names such as
totalMarksinstead ofx. - Keep arithmetic expressions simple and readable.
- Use the ternary operator only for short, straightforward conditions.
- Learn operator precedence, but use parentheses whenever the evaluation order may be unclear.
Module Summary
In this module, you learned:
- What operators are and how they manipulate data.
- Arithmetic operators for mathematical calculations.
- Relational operators for comparing values.
- Logical operators for combining conditions.
- Assignment operators for updating variables efficiently.
- The difference between pre/post increment and decrement operators.
- How bitwise operators manipulate binary data.
- How to simplify conditional expressions using the ternary operator.
- The importance of operator precedence and associativity.
- How to apply these concepts by building a calculator, an even/odd checker, and a largest number finder.
After completing this module, you'll be ready to learn control statements (if, switch, loops, break, and continue), enabling your programs to make decisions and execute repetitive tasks efficiently.