Operators in C
Operators are special symbols that perform operations on variables and values. Think of them as the tools in your programming toolbox - each one has a specific job to do!
Following are the different types of Operators
1. Arithmetic Operators
These operators perform basic mathematical operations.
| Operator | Description | Example |
|---|---|---|
+ | Addition | 5 + 3 = 8 |
- | Subtraction | 5 - 3 = 2 |
* | Multiplication | 5 * 3 = 15 |
/ | Division | 6 / 3 = 2 |
% | Modulus (remainder) | 7 % 3 = 1 |
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("Addition: %d + %d = %d\n", a, b, a + b);
printf("Subtraction: %d - %d = %d\n", a, b, a - b);
printf("Multiplication: %d * %d = %d\n", a, b, a * b);
printf("Division: %d / %d = %d\n", a, b, a / b);
printf("Modulus: %d %% %d = %d\n", a, b, a % b);
return 0;
}
2. Assignment Operators
These operators assign values to variables.
| Operator | Description | Example | Equivalent |
|---|---|---|---|
= | Simple assignment | x = 5 | - |
+= | Add and assign | x += 3 | x = x + 3 |
-= | Subtract and assign | x -= 2 | x = x - 2 |
*= | Multiply and assign | x *= 4 | x = x * 4 |
/= | Divide and assign | x /= 2 | x = x / 2 |
#include <stdio.h>
int main() {
int x = 10;
printf("Initial value: %d\n", x);
x += 5; // x = x + 5
printf("After x += 5: %d\n", x);
x *= 2; // x = x * 2
printf("After x *= 2: %d\n", x);
return 0;
}
3. Comparison Operators
These operators compare two values and return either true (1) or false (0).
| Operator | Description | Example |
|---|---|---|
== | Equal to | 5 == 5 returns 1 |
!= | Not equal to | 5 != 3 returns 1 |
> | Greater than | 5 > 3 returns 1 |
< | Less than | 3 < 5 returns 1 |
>= | Greater than or equal | 5 >= 5 returns 1 |
<= | Less than or equal | 3 <= 5 returns 1 |
#include <stdio.h>
int main() {
int a = 5, b = 3;
printf("%d == %d is %d\n", a, b, a == b);
printf("%d > %d is %d\n", a, b, a > b);
printf("%d < %d is %d\n", a, b, a < b);
return 0;
}
4. Logical Operators
These operators perform logical operations and are commonly used in conditions.
| Operator | Description | Example |
|---|---|---|
&& | Logical AND | (5 > 3) && (2 < 4) returns 1 |
|| | Logical OR | (5 < 3) || (2 < 4) returns 1 |
! | Logical NOT | !(5 > 3) returns 0 |
#include <stdio.h>
int main() {
int age = 20;
int hasLicense = 1; // 1 means true, 0 means false
// Can drive if age >= 18 AND has license
if (age >= 18 && hasLicense) {
printf("Can drive!\n");
}
// Weekend if day is Saturday OR Sunday
int day = 6; // Let's say 6 = Saturday, 7 = Sunday
if (day == 6 || day == 7) {
printf("It's weekend!\n");
}
return 0;
}
5. Increment and Decrement Operators
These operators increase or decrease a variable's value by 1.
| Operator | Description | Example |
|---|---|---|
++ | Increment by 1 | x++ or ++x |
-- | Decrement by 1 | x-- or --x |
#include <stdio.h>
int main() {
int count = 5;
printf("Initial count: %d\n", count);
count++; // Post-increment
printf("After count++: %d\n", count);
++count; // Pre-increment
printf("After ++count: %d\n", count);
count--; // Post-decrement
printf("After count--: %d\n", count);
return 0;
}
Operator Precedence
Just like in mathematics, operators have an order of precedence. Multiplication and division happen before addition and subtraction, unless you use parentheses to change the order.
#include <stdio.h>
int main() {
int result1 = 2 + 3 * 4; // Result: 14 (not 20)
int result2 = (2 + 3) * 4; // Result: 20
printf("2 + 3 * 4 = %d\n", result1);
printf("(2 + 3) * 4 = %d\n", result2);
return 0;
}
Remember: When in doubt, use parentheses to make your intentions clear! It makes your code more readable and prevents unexpected results.