**Operators** are symbols that operate on values or variables (called operands). For example, in the expression 10 + 5, the + is the operator and 10 and 5 are the operands. C provides a rich set of operators to handle everything from basic math to complex bitwise manipulation.
To make learning easier, C operators are classified into several groups based on the type of operation they perform:
These are used to perform common mathematical operations. Most of these are familiar from basic algebra, but the Modulus operator is unique to programming.
| Operator | Name | Example |
|---|---|---|
+ |
Addition | x + y |
- |
Subtraction | x - y |
* |
Multiplication | x * y |
/ |
Division | x / y |
% |
Modulus | x % y (Returns the remainder) |
++ |
Increment | Increases value by 1 |
-- |
Decrement | Decreases value by 1 |
The assignment operator (=) is used to assign a value to a variable. C also supports Compound Assignment Operators, which perform an operation and an assignment in one step.
Comparison operators compare two values and return either **1 (true)** or **0 (false)**. These are essential for decision-making in programs (like if statements).
== : Equal to!= : Not equal to> : Greater than< : Less than>= : Greater than or equal toLogical operators are used to determine the logic between variables or values. They are usually used to combine multiple comparisons.
[Image of logical operators truth table: AND, OR, NOT]| Operator | Name | Logic |
|---|---|---|
&& |
Logical AND | Returns true if both statements are true. |
|| |
Logical OR | Returns true if one of the statements is true. |
! |
Logical NOT | Reverse the result (returns false if result is true). |
In an expression with multiple operators, C follows a specific order called **Operator Precedence** to decide which operation happens first. For example, multiplication (*) has higher precedence than addition (+).
Let's see how the modulus and increment operators work in a real scenario:
() when writing complex expressions. Even if you know the precedence rules, using parentheses makes your code much easier to read for others and prevents accidental logic errors.