Operators in C
An operator is a symbol that tells the compiler to perform a specific operation on one or more operands (variables or values).Arithmetic Operators
Used to perform mathematical operations (assumea = 10, b = 3):
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | a + b | 13 |
- | Subtraction | a - b | 7 |
* | Multiplication | a * b | 30 |
/ | Division | a / b | 3 |
% | Modulus | a % b | 1 |
/ performs integer division when both operands are integers (drops the decimal). % gives the remainder of that division.
Assignment Operators
Assign values to variables. The right-hand side is evaluated first, then assigned to the left.Relational Operators
Compare two values. Result is1 (true) or 0 (false). Assume a = 10, b = 5:
| Operator | Description | Example | Result |
|---|---|---|---|
== | Equal to | a == b | 0 |
!= | Not equal to | a != b | 1 |
> | Greater than | a > b | 1 |
< | Less than | a < b | 0 |
>= | Greater or equal | a >= b | 1 |
<= | Less or equal | a <= b | 0 |
Logical Operators
Combine multiple conditions:| Operator | Name | Meaning |
|---|---|---|
&& | Logical AND | True if both are true |
|| | Logical OR | True if either is true |
! | Logical NOT | Inverts the condition |
Bitwise Operators
Operate at the binary (bit) level:| Operator | Name | Description |
|---|---|---|
& | AND | 1 if both bits are 1 |
| | OR | 1 if either bit is 1 |
^ | XOR | 1 if bits differ |
~ | NOT | Flips all bits |
<< | Left Shift | Shifts bits left |
>> | Right Shift | Shifts bits right |
Increment / Decrement Operators
| Operator | Type | Effect |
|---|---|---|
++a | Pre-increment | Increment first, then use |
a++ | Post-increment | Use first, then increment |
--a | Pre-decrement | Decrement first, then use |
a-- | Post-decrement | Use first, then decrement |