Categories of Operators in C
In C programming, operators are symbols that perform operations on variables and values. These operations can be arithmetic, logical, relational, bitwise, or assignment in nature. Understanding how to use these operators effectively is crucial for writing efficient and effective C programs. Below, we will explore the different categories of operators in C and provide examples for each.
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
- `+` : Addition
- `-` : Subtraction
- `*` : Multiplication
- `/` : Division
- `%` : Modulus (remainder after division)
Example:
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("Addition: %d\n", a + b); // 13
printf("Subtraction: %d\n", a - b); // 7
printf("Multiplication: %d\n", a * b); // 30
printf("Division: %d\n", a / b); // 3
printf("Modulus: %d\n", a % b); // 1
return 0;
}
2. Relational Operators
Relational operators are used to compare two values.
- `==` : Equal to
- `!=` : Not equal to
- `>` : Greater than
- `<` : Less than
- `>=` : Greater than or equal to
- `<=` : Less than or equal to
Example:
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("a == b: %d\n", a == b); // 0 (false)
printf("a != b: %d\n", a != b); // 1 (true)
printf("a > b: %d\n", a > b); // 1 (true)
printf("a < b: %d\n", a < b); // 0 (false)
printf("a >= b: %d\n", a >= b); // 1 (true)
printf("a <= b: %d\n", a <= b); // 0 (false)
return 0;
}
3. Logical Operators
Logical operators are used to perform logical operations.
- `&&` : Logical AND
- `||` : Logical OR
- `!` : Logical NOT
Example:
#include <stdio.h>
int main() {
int a = 1, b = 0;
printf("a && b: %d\n", a && b); // 0 (false)
printf("a || b: %d\n", a || b); // 1 (true)
printf("!a: %d\n", !a); // 0 (false)
printf("!b: %d\n", !b); // 1 (true)
return 0;
}
4. Bitwise Operators
Bitwise operators are used to perform bit-level operations.
- `&` : Bitwise AND
- `|` : Bitwise OR
- `^` : Bitwise XOR
- `~` : Bitwise NOT
- `<<` : Left shift
- `>>` : Right shift
Example:
#include <stdio.h>
int main() {
int a = 5, b = 3; // Binary of a = 0101, b = 0011
printf("a & b: %d\n", a & b); // 1 (0001)
printf("a | b: %d\n", a | b); // 7 (0111)
printf("a ^ b: %d\n", a ^ b); // 6 (0110)
printf("~a: %d\n", ~a); // -6 (invert all bits of 0101)
printf("a << 1: %d\n", a << 1); // 10 (1010)
printf("a >> 1: %d\n", a >> 1); // 2 (0010)
return 0;
}
5. Assignment Operators
Assignment operators are used to assign values to variables.
- `=` : Assign
- `+=` : Add and assign
- `-=` : Subtract and assign
- `*=` : Multiply and assign
- `/=` : Divide and assign
- `%=` : Modulus and assign
Example:
#include <stdio.h>
int main() {
int a = 10;
a += 5; // a = a + 5
printf("a += 5: %d\n", a); // 15
a -= 3; // a = a - 3
printf("a -= 3: %d\n", a); // 12
a *= 2; // a = a * 2
printf("a *= 2: %d\n", a); // 24
a /= 4; // a = a / 4
printf("a /= 4: %d\n", a); // 6
a %= 5; // a = a % 5
printf("a %%= 5: %d\n", a); // 1
return 0;
}
6. Increment and Decrement Operators
These operators are used to increase or decrease the value of a variable by 1.
- `++` : Increment
- `--` : Decrement
Example:
#include <stdio.h>
int main() {
int a = 5;
printf("a: %d\n", a); // 5
a++;
printf("a++: %d\n", a); // 6
a--;
printf("a--: %d\n", a); // 5
++a;
printf("++a: %d\n", a); // 6
--a;
printf("--a: %d\n", a); // 5
return 0;
}
7. Conditional (Ternary) Operator
This operator is a shorthand way of writing an `if-else` statement.
- `? :` : Conditional operator
Example:
#include <stdio.h>
int main() {
int a = 5, b = 10;
int max = (a > b) ? a : b;
printf("Max value: %d\n", max); // 10
return 0;
}
8. sizeof Operator
The `sizeof` operator is used to determine the size of a data type or a variable.
Example:
#include <stdio.h>
int main() {
int a;
double b;
printf("Size of int: %zu bytes\n", sizeof(a)); // Typically 4 bytes
printf("Size of double: %zu bytes\n", sizeof(b)); // Typically 8 bytes
return 0;
}
0 Comments