Operators in C: Definition, List, and Examples
In C programming, an operator is a special symbol used to perform operations on variables and values. Operators are the building blocks of expressions and help in performing calculations, comparisons, and logical decisions.
int a = 10, b = 5;
int sum = a + b; // '+' is the operator
Complete List of Operators in C
- Arithmetic Operators: +, -, *, /, %
- Relational Operators: ==, !=, >, <, >=, <=
- Logical Operators: &&, ||, !
- Assignment Operators: =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
- Increment/Decrement Operators: ++, —
- Bitwise Operators: &, |, ^, ~, <<, >>
- Conditional (Ternary) Operator: ? :
- Special Operators: sizeof, &, *, ., ->, , (comma)
Operators in C with Examples
1. Arithmetic Operators
| Operator | Description | Example (a=10, b=5) | Result |
| + | Addition | a + b | 15 |
| – | Subtraction | a – b | 5 |
| * | Multiplication | a * b | 50 |
| / | Division | a / b | 2 |
| % | Modulus | a % b | 0 |
2. Relational Operators
| Operator | Description | Example (a=10, b=5) | Result |
| == | Equal to | a == b | 0 (false) |
| != | Not equal to | a != b | 1 (true) |
| > | Greater than | a > b | 1 (true) |
| < | Less than | a < b | 0 (false) |
| >= | Greater or equal | a >= b | 1 (true) |
| <= | Less or equal | a <= b | 0 (false) |
3. Logical Operators
| Operator | Description | Example | Result |
| && | Logical AND | (a > 5 && b > 2) | 1 (true) |
| || | Logical OR | (a > 5 || b > 10) | 1 (true) |
| ! | Logical NOT | !(a > b) | 0 (false) |
4. Assignment Operators
| Operator | Example | Meaning |
| = | a = 10 | Assign 10 to a |
| += | a += 5 | a = a + 5 |
| -= | a -= 5 | a = a – 5 |
| *= | a *= 5 | a = a * 5 |
| /= | a /= 5 | a = a / 5 |
| %= | a %= 5 | a = a % 5 |
| &= | a &= 2 | a = a & 2 |
| |= | a |= 2 | a = a | 2 |
| ^= | a ^= 2 | a = a ^ 2 |
| <<= | a <<= 1 | a = a << 1 |
| >>= | a >>= 1 | a = a >> 1 |
5. Increment and Decrement Operators
| Operator | Description | Example (a=10) | Result |
| ++a | Pre-increment | ++a | 11 |
| a++ | Post-increment | a++ | 10 (then a=11) |
| –a | Pre-decrement | –a | 9 |
| a– | Post-decrement | a– | 10 (then a=9) |
6. Bitwise Operators
| Operator | Description | Example (a=5, b=3) | Result |
| & | Bitwise AND | a & b | 1 |
| | | Bitwise OR | a | b | 7 |
| ^ | Bitwise XOR | a ^ b | 6 |
| ~ | Bitwise NOT | ~a | -6 |
| << | Left shift | a << 1 | 10 |
| >> | Right shift | a >> 1 | 2 |
7. Conditional (Ternary) Operator
Syntax:
condition ? expression1 : expression2;
Example:
int result = (a > b) ? a : b; // returns max of a and b
8. Special Operators
- sizeof → returns size of a data type or variable
- & → address of variable
- * → pointer (value at address)
- . → access structure member
- -> → access structure member through pointer
- , → comma operator (separates expressions)