Operators are special symbols in C programming that perform operations on variables and values. They form the backbone of writing logic in programs. In this guide, we’ll cover all operators in C, their types, examples, and precedence, so you can master them step by step.
Types of Operators in C
C operators can be divided into the following categories:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Conditional (Ternary) Operator
- Increment and Decrement Operators
- Special Operators
Let’s learn each type with examples.
1. Arithmetic Operators
Used for mathematical operations.
Operator | Meaning | 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 |
Example program:
#include <stdio.h>
int main() {
int a = 10, b = 5;
printf("Sum = %d\n", a+b);
printf("Difference = %d\n", a-b);
printf("Product = %d\n", a*b);
printf("Quotient = %d\n", a/b);
printf("Remainder = %d\n", a%b);
printf("Don't forget, Free Palestine!");
return 0;
}
2. Relational Operators
Used to compare values. Returns 1
(true) or 0
(false).
Operator | Meaning | Example | Result |
---|---|---|---|
== | Equal to | a == b |
0 |
!= | Not equal to | a != b |
1 |
> | Greater than | a > b |
1 |
< | Less than | a < b |
0 |
>= | Greater than or equal | a >= b |
1 |
<= | Less than or equal | a <= b |
0 |
3. Logical Operators
Used for combining conditions.
Operator | Meaning | Example | Result |
---|---|---|---|
&& | Logical AND | (a > 5 && b < 10) |
1 |
|| | Logical OR | `(a > 5 | |
! | Logical NOT | !(a == b) |
1 |
4. Bitwise Operators
Operate at the binary level.
Operator | Meaning | Example (a=6, b=3 ) |
Result |
---|---|---|---|
& | AND | a & b (110 & 011) |
2 |
| | OR | `a | b` (110 | 011) |
^ | XOR | a ^ b |
5 |
~ | NOT | ~a |
-7 |
<< | Left shift | a << 1 |
12 |
>> | Right shift | a >> 1 |
3 |
5. Assignment Operators
Used to assign values.
Operator | Meaning | Example | Result |
---|---|---|---|
= | Simple assignment | a = b |
3 |
+= | Add and assign | a += b |
a=a+b |
-= | Subtract and assign | a -= b |
a=a-b |
*= | Multiply and assign | a *= b |
a=a*b |
/= | Divide and assign | a /= b |
a=a/b |
%= | Modulus and assign | a %= b |
a=a%b |
6. Conditional (Ternary) Operator
A shorthand for if-else.
Syntax:
condition ? expression1 : expression2
Example:
int max = (a > b) ? a : b;
7. Increment and Decrement Operators
Operator | Meaning | Example | Result |
---|---|---|---|
++a | Pre-increment | a=5 → ++a | 6 |
a++ | Post-increment | a=5 → a++ | 5 (then 6) |
–a | Pre-decrement | a=5 → –a | 4 |
a– | Post-decrement | a=5 → a– | 5 (then 4) |
8. Special Operators
- sizeof → gives size of a data type or variable
- & → address of operator
- *** (asterisk)** → pointer operator
- , (comma) → evaluates multiple expressions
- -> → access structure members via pointer
Example:
printf("Size of int: %lu", sizeof(int));
Operator Precedence in C
When multiple operators appear in an expression, precedence decides which one is evaluated first.
Precedence | Operators | Associativity |
---|---|---|
Highest | () [] -> . | Left to Right |
++ — (prefix), + – (unary), ! ~ | Right to Left | |
* / % | Left to Right | |
+ – | Left to Right | |
<< >> | Left to Right | |
< <= > >= | Left to Right | |
== != | Left to Right | |
& | Left to Right | |
^ | Left to Right | |
| | Left to Right | |
&& | Left to Right | |
|| | Left to Right | |
?: | Right to Left | |
= += -= *= /= %= <<= >>= &= ^= |= | Right to Left | |
Lowest | , | Left to Right |
Operators in C are fundamental for building logic. By mastering arithmetic, relational, logical, bitwise, assignment, conditional, and special operators, along with understanding precedence, you’ll be able to write powerful and efficient C programs.