1. Types of Operators
C has a rich set of operators categorized by what they do.
A. Arithmetic Operators
Used for mathematical calculations.
| Operator | Description | Example ($a=10, b=3$) | Result |
| :--- | :--- | :--- | :--- |
| + | Addition | a + b | 13 |
| - | Subtraction | a - b | 7 |
| * | Multiplication | a * b | 30 |
| / | Division | a / b | 3 (Integer division) |
| % | Modulus | a % b | 1 (Remainder) |
B. Relational & Logical Operators
Used for decision-making (True/False). In C, 0 is False and any non-zero value is True.
Relational:
==(equal to),!=(not equal),>,<,>=,<=Logical:
&&(AND),||(OR),!(NOT)
C. Increment and Decrement
++x: Pre-increment (Add 1, then use).x++: Post-increment (Use, then add 1).1. Types of OperatorsC has a rich set of operators categorized by what they do.
A. Arithmetic Operators
Used for mathematical calculations.
| Operator | Description | Example ($a=10, b=3$) | Result |
| :--- | :--- | :--- | :--- |
|
+| Addition |a + b| 13 ||
-| Subtraction |a - b| 7 ||
*| Multiplication |a * b| 30 ||
/| Division |a / b| 3 (Integer division) ||
%| Modulus |a % b| 1 (Remainder) |B. Relational & Logical Operators
Used for decision-making (True/False). In C, 0 is False and any non-zero value is True.
Relational:
==(equal to),!=(not equal),>,<,>=,<=Logical:
&&(AND),||(OR),!(NOT)
C. Increment and Decrement
++x: Pre-increment (Add 1, then use).
#include <stdio.h>
int main() {
int a = 10, b = 4;
int result;
// Arithmetic expression
result = a * b + 2; // (10 * 4) + 2 = 42
printf("Result 1: %d\n", result);
// Using Modulus (Remainder)
int remainder = a % b; // 10 divided by 4 leaves 2
printf("Remainder: %d\n", remainder);
// Logical expression
// Is 'a' greater than 5 AND 'b' less than 10?
if (a > 5 && b < 10) {
printf("Both conditions are true!\n");
}
// Increment
int x = 5;
printf("Post-increment: %d\n", x++); // Prints 5, then becomes 6
printf("Value now: %d\n", x);
return 0;
}Expected Output:
Result 1: 42
Remainder: 2
Both conditions are true!
Post-increment: 5
Value now: 63. Type Casting in Expressions
Sometimes you need to change a data type temporarily to get the right result in an expression. This is called Type Casting.
int total = 10, count = 4;
float average;
// Without casting: 10 / 4 = 2.0 (because both are ints)
// With casting: (float)10 / 4 = 2.5
average = (float)total / count;Expected Output:
Bad Average: 2.0
Good Average: 2.5Key Tip: Assignment is an Operator
In C, = is an operator that returns a value. This is why you can do: x = y = z = 100; It assigns 100 to z, then that result to y, then that result to x.