Operators are the symbols used to perform specific operations on data. Expressions are the combinations of variables, constants, and operators that produce values. Together, they are the core of problem-solving in C programming.
1. Arithmetic Operators
Used for mathematical calculations.
| Operator | Meaning | Example | Result |
| + | Addition | 5 + 3 | 8 |
| – | Subtraction | 5 – 3 | 2 |
| * | Multiplication | 5 * 3 | 15 |
| / | Division | 5 / 2 | 2 |
| % | Modulus (remainder) | 5 % 2 | 1 |
Note: In integer division, decimal part is ignored.
2. Relational Operators
Used to compare values, the result is either true (1) or false (0).
| Operator | Meaning | Example | Result |
| == | Equal to | 5 == 3 | 0 |
| != | Not equal to | 5 != 3 | 1 |
| > | Greater than | 5 > 3 | 1 |
| < | Less than | 5 < 3 | 0 |
| >= | Greater or equal | 5 >= 5 | 1 |
| <= | Less or equal | 5 <= 6 | 1 |
3. Logical Operators
Used for decision making with boolean values.
| Operator | Meaning | Example | Result |
| && | Logical AND | (5 > 2 && 5 < 10) | 1 |
| ` | ` | Logical OR | |
| ! | Logical NOT | !(5 > 2) | 0 |
4. Assignment Operator
Used to assign values.
| Operator | Meaning | Example | Equivalent |
| = | Assign | a = 10 | Assigns 10 |
| += | Add and assign | a += 5 | a = a + 5 |
| -= | Subtract and assign | a -= 5 | a = a – 5 |
| *= | Multiply and assign | a *= 5 | a = a * 5 |
| /= | Divide and assign | a /= 5 | a = a / 5 |
5. Increment / Decrement Operator
- ++ → Increases value by 1.
- — → Decreases value by 1.
int a = 5;
printf(“%d”, ++a); // Output: 6 (pre-increment)
printf(“%d”, a++); // Output: 6 then a becomes 7 (post-increment)
6. Conditional Operator (?:)
Shorthand for if-else.
Syntax:
condition ? expression1 : expression2;
Example:
int a = 5, b = 10, max;
max = (a > b) ? a : b;
printf(“%d”, max); // Output: 10
7. Bitwise Operators
Work at the bit level (0s and 1s).
| Operator | Meaning | Example (a=5=0101, b=3=0011) | Result |
| & | AND | a & b → 0101 & 0011 | 1 |
| ` | ` | OR | `a |
| ^ | XOR | a ^ b → 0101 ^ 0011 | 6 |
| ~ | NOT | ~a → Inverts bits of a | -6 |
| << | Left shift | a << 1 → 0101 → 1010 | 10 |
| >> | Right shift | a >> 1 → 0101 → 0010 | 2 |
8. Comma Operator
- Evaluates multiple expressions but returns the value of the last expression.
int x;
x = (1, 2, 3); // x = 3
9. sizeof Operator
Returns the size of a data type (in bytes).
printf(“%d”, sizeof(int)); // Output: 4 (depends on system)
printf(“%d”, sizeof(char)); // Output: 1
10. Operator Precedence & Associativity
When multiple operators are in an expression, precedence decides priority and associativity decides direction.
Example:
int x = 5 + 3 * 2; // Multiplication first → 5 + 6 = 11
Precedence Order (Highest → Lowest):
- (), [] (Parentheses, Array indexing)
- ++, –, !, ~, sizeof
- *, /, %
- +, –
- <, <=, >, >=
- ==, !=
- &&
- ||
- ?:
- =, +=, -=
11. Expressions and Evaluation
An expression is a combination of variables, constants, and operators.
Example:
int x = (5 + 3) * 2; // Evaluated as 16
12. Type Casting in Expression
Converting one data type into another.
Implicit Casting: Done automatically.
int a = 5;
float b = a; // int → float
Explicit Casting: Done manually.
float avg;
int sum = 5, n = 2;
avg = (float) sum / n; // Correct → 2.5
13. Program Statement
- Statement: Smallest independent executable unit in C.
- Types:
- Expression Statement → x = 5 + 2;
- Compound Statement → { a = 2; b = 3; }
- Control Statement → if, for, while, switch
- Expression Statement → x = 5 + 2;
Key Takeaways
- Operators are the building blocks of expressions in C.
- Categories include arithmetic, relational, logical, assignment, increment/decrement, conditional, bitwise, comma, and sizeof.
- Operator precedence & associativity decide evaluation order.
- Expressions can be type casted to avoid errors in calculation.
Understanding operators helps in writing efficient and bug-free programs.