Operators in C
Operators are special symbols in C that perform operations on variables and values. Understanding operators is essential for writing effective C programs. This guide will cover the various types of operators available in C.
1. Arithmetic Operators​
Arithmetic operators are used to perform basic mathematical operations.
Operator | Description | Example |
---|---|---|
+ | Addition | int sum = a + b; |
- | Subtraction | int diff = a - b; |
* | Multiplication | int prod = a * b; |
/ | Division | int quot = a / b; |
% | Modulus (Remainder) | int rem = a % b; |
Example:​
int a = 10, b = 3;
int sum = a + b; // 13
int diff = a - b; // 7
int prod = a * b; // 30
int quot = a / b; // 3
int rem = a % b; // 1
2. Relational Operators​
Relational operators compare two values and return a boolean result (true
or false
).
Operator | Description | Example |
---|---|---|
== | Equal to | x == y |
!= | Not equal to | x != y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater than or equal to | x >= y |
<= | Less than or equal to | x <= y |
Example:​
#include <stdio.h>
int main() {
int x = 10, y = 5;
printf("%d\n", (x == y)); // Output: 0 (false)
printf("%d\n", (x != y)); // Output: 1 (true)
printf("%d\n", (x > y)); // Output: 1 (true)
printf("%d\n", (x < y)); // Output: 0 (false)
printf("%d\n", (x >= y)); // Output: 1 (true)
printf("%d\n", (x <= y)); // Output: 0 (false)
return 0;
}
3. Logical Operators​
Logical operators are used to perform logical operations and combine multiple conditions.
Operator | Description | Example |
---|---|---|
&& | Logical AND | (x > 5 && y < 10) |
! | Logical NOT | !(x > 5) |
Example:​
#include <stdio.h>
int main() {
int x = 10, y = 5;
printf("%d\n", (x > 5 && y < 10)); // Output: 1 (true)
printf("%d\n", (x > 5 || y > 10)); // Output: 1 (true)
printf("%d\n", !(x > 5)); // Output: 0 (false)
return 0;
}
4. Assignment Operators​
Assignment operators are used to assign values to variables.
Operator | Description | Example |
---|---|---|
= | Assigns value | x = y |
+= | Adds and assigns | x += y |
-= | Subtracts and assigns | x -= y |
*= | Multiplies and assigns | x *= y |
/= | Divides and assigns | x /= y |
%= | Modulus and assigns | x %= y |
Example:​
#include <stdio.h>
int main() {
int x = 10, y = 5;
x += y; // Equivalent to x = x + y
printf("%d\n", x); // Output: 15
return 0;
}
5. Increment and Decrement Operators​
Increment and decrement operators are used to increase or decrease a variable's value by 1.
Operator | Description | Example |
---|---|---|
++ | Increments value | ++x or x++ |
-- | Decrements value | --x or x-- |
Example:​
#include <stdio.h>
int main() {
int x = 10;
printf("%d\n", ++x); // Output: 11 (Pre-increment)
printf("%d\n", x--); // Output: 11 (Post-decrement)
printf("%d\n", x); // Output: 10
return 0;
}
6. Bitwise Operators​
Bitwise operators operate on bits and perform bit-level operations.
| Operator | Description | Example | | -------- | ----------- | ---------- | --- | --- | | & | Bitwise AND | x & y | | | | Bitwise OR | x | y | | ^ | Bitwise XOR | x ^ y | | ~ | Bitwise NOT | ~x | | >> | Left shift | x >> 2 | | << | Right shift | x << 2 |
Example:​
#include <stdio.h>
int main() {
int x = 5, y = 9;
printf("%d\n", (x & y)); // Output: 1
printf("%d\n", (x | y)); // Output: 13
return 0;
}
7. Ternary Operator​
The ternary operator is a shorthand for an if-else statement.
Operator | Description | Example |
---|---|---|
?: | Ternary | condition ? expr1 : expr2 |
Example:​
#include <stdio.h>
int main() {
int x = 10;
int result = (x > 5) ? 100 : 200;
printf("%d\n", result); // Output: 100
return 0;
}
Understanding these operators is key to mastering C programming and writing efficient code!