Operators are symbols that perform operations on variables and values. Let's explore the main types:
Used for basic mathematical operations.
| Operator | Description | Example |
|---|---|---|
| + | Addition | a + b |
| - | Subtraction | a - b |
| * | Multiplication | a * b |
| / | Division | a / b |
| % | Modulus (remainder) | a % b |
int a = 10, b = 3;
printf("%d", a + b); // Output: 13
Used to compare two values. Returns 1 if true, 0 if false.
| Operator | Description | Example |
|---|---|---|
| == | Equal to | a == b |
| != | Not equal to | a != b |
| > | Greater than | a > b |
| < | Less than | a < b |
| >= | Greater than or equal to | a >= b |
| <= | Less than or equal to | a <= b |
int a = 5, b = 10;
printf("%d", a < b); // Output: 1 (true)
Used to combine conditional expressions.
| Operator | Description | Example |
|---|---|---|
| && | Logical AND | (a > 0) && (b > 0) |
| || | Logical OR | (a > 0) || (b > 0) |
| ! | Logical NOT | !(a > 0) |
int a = 5, b = -1;
printf("%d", (a > 0) && (b > 0)); // Output: 0 (false)
Used to perform operations on bits.
| Operator | Description | Example |
|---|---|---|
| & | Bitwise AND | a & b |
| | | Bitwise OR | a | b |
| ^ | Bitwise XOR | a ^ b |
| ~ | Bitwise NOT | ~a |
| << | Left Shift | a << 2 |
| >> | Right Shift | a >> 2 |
int a = 5, b = 3;
printf("%d", a & b); // Output: 1 (0101 & 0011 = 0001)