- What are operators and expressions in C
- What are arithmetic operators
- What are relational and logical operators
- What are assignment and bitwise operators
- What are increment/decrement operators
- What are special and conditional operators
What are Operators and Expressions?
Operators are special symbols that tell the compiler to perform specific mathematical or logical operations. Think of them like verbs in a sentence — they do the action.
An Expression is a combination of operators and operands (values or variables) that produces a result. For example, a + b is an expression where + is the operator and a, b are operands.
💡 Quick Definition: An operator is a symbol that performs an operation. An expression is a combination of operators and operands that yields a value.
C supports 8 types of operators:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
- Bitwise Operators
- Increment/Decrement Operators
- Special Operators
- Conditional Operator
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, division, and modulus.
| Operator | Operation | Example | Description |
|---|---|---|---|
+ | Addition | a + b | Adds two numbers |
- | Subtraction | a - b | Subtracts two numbers |
* | Multiplication | a * b | Multiplies two numbers |
/ | Division | a / b | Divides two numbers |
% | Modulo | a % b | Returns remainder |
2. Relational Operators
Relational operators compare two values and return true (1) or false (0). They're used in decision-making and loops.
| Symbol | Operation | Example | Description |
|---|---|---|---|
< | Less than | a < b | True if a is less than b |
> | Greater than | a > b | True if a is greater than b |
== | Equal to | a == b | True if a is equal to b |
!= | Not equal | a != b | True if a is not equal to b |
<= | Less than or equal | a <= b | True if a is less than or equal to b |
>= | Greater than or equal | a >= b | True if a is greater than or equal to b |
3. Logical Operators
Logical operators are used to combine two or more conditions. They're essential for complex decision-making.
| Symbol | Operation | Example | Description |
|---|---|---|---|
&& | Logical AND | (x > 5) && (y < 5) | True when both conditions are true |
|| | Logical OR | (x >= 10) || (y >= 10) | True when at least one condition is true |
! | Logical NOT | !(x >= 5) | True when condition is false |
4. Assignment Operators
Assignment operators are used to assign values to variables. The most common is =, but C also provides compound assignment operators.
| Symbol | Operation | Example | Equivalent |
|---|---|---|---|
= | Assignment | a = 5 | Assigns 5 to a |
+= | Addition assignment | a += 5 | a = a + 5 |
-= | Subtraction assignment | a -= 5 | a = a - 5 |
*= | Multiplication assignment | a *= 5 | a = a * 5 |
/= | Division assignment | a /= 5 | a = a / 5 |
%= | Modulo assignment | a %= 5 | a = a % 5 |
5. Bitwise Operators
Bitwise operators perform operations at the bit level. They're used for low-level programming and optimization.
| Symbol | Operation | Example | Description |
|---|---|---|---|
& | Bitwise AND | a & b | AND operation on bits |
| | Bitwise OR | a | b | OR operation on bits |
^ | Bitwise XOR | a ^ b | XOR operation on bits |
~ | Bitwise NOT | ~a | Complement of bits |
<< | Left shift | a << b | Shifts bits left |
>> | Right shift | a >> b | Shifts bits right |
6. Increment/Decrement Operators
Increment (++) and decrement (--) operators are unary operators that increase or decrease the value of a variable by 1.
Prefix vs Postfix:
++a (Prefix) — Increments first, then uses the value
a++ (Postfix) — Uses the value first, then increments
a++; // a becomes 6 (post-increment)
++a; // a becomes 7 (pre-increment)
a--; // a becomes 6 (post-decrement)
--a; // a becomes 5 (pre-decrement)
7. Special Operators
C provides two special operators:
&(Address of Operator): Returns the memory address of a variable. Example:&agives the address of variablea.*(Pointer Operator): Used to declare pointers and dereference them. Example:*ptraccesses the value at the address stored inptr.
8. Conditional Operator (?:)
The conditional operator (also called the ternary operator) is a shorthand for if-else. It takes three operands.
Syntax: condition ? expression1 : expression2;
If condition is true, expression1 is evaluated; otherwise, expression2 is evaluated.
int max = (a > b) ? a : b;
// max will be 20 because a > b is false
printf("Maximum value is: %d", max);
Frequently Asked Questions About Operators in C
1. What is the difference between = and == in C?
= is the assignment operator used to assign values. == is the equality operator used to compare values.
2. What is the difference between prefix and postfix increment?
Prefix (++a): Increments first, then returns the new value. Postfix (a++): Returns the original value first, then increments.
3. What is the ternary operator?
The ternary operator (?:) is a shorthand for if-else. It takes three operands: condition, true result, false result.
4. Why are bitwise operators useful?
Bitwise operators are useful for low-level programming, optimization, and tasks like setting/clearing specific bits in a number.
💡 Quick tip: Operators are the building blocks of C expressions. Mastering them is essential for writing efficient, readable code.