Operators in C Programming
What are Operators in C
Operators are special symbols that perform specific operations on one, two, or more operands, and then return a result.
For example:
The addition operator
+
performs addition on two operands and the assignment operator=
assigns a value to a variable.An operand is a variable, constant or expression that is operated upon by an operator.
C has a rich set of operators, including arithmetic operators, relational operators, logical operators, bitwise operators, and more.
Types of operators in C
Types of operators in C include:
- Arithmetic Operators
- Relational/Comparison Operators
- Logical Operators
- Assignment Operators
- Ternary Operator
- Bitwise Operators
- Special Operators
- Increment and Decrement Operators
Table of Operators in C
Below is a table of common operators in C, grouped by type:
Type of Operator | Operator | Example | Result |
---|---|---|---|
Arithmetic | + | 5 + 10 | 15 |
- | 10 - 5 | 5 | |
* | 5 * 15 | 75 | |
/ | 15 / 5 | 3 | |
% | 7 % 3 | 1 | |
Comparison | == | 5 == 5 | true |
!= | 5 != 10 | true | |
> | 5 > 10 | false | |
< | 5 < 10 | true | |
>= | 5 >= 5 | true | |
<= | 5 <= 10 | true | |
Logical | && | (5 > 2) && (10 < 20) | true |
|| | (5 > 2) || (10 < 5) | true | |
! | !(5 > 10) | true | |
Assignment | = | x = 5 | x is 5 |
+= | x += 5 | x is 10 | |
-= | x -= 5 | x is 5 | |
*= | x *= 5 | x is 25 | |
/= | x /= 5 | x is 5 | |
Increment/Decrement | ++ | x++ | x is 6 |
-- | x-- | x is 5 | |
Bitwise | & | x & y | bitwise and of x and y |
| | x | y | bitwise or of x and y | |
^ | x ^ y | bitwise xor of x and y | |
~ | ~x | bitwise complement of x | |
<< | x << y | bitwise left shift of x by y | |
>> | x >> y | bitwise right shift of x by y | |
Special | sizeof | sizeof(x) | size of x in bytes |
& | &x | address of x | |
* | *x | value at the address x | |
. | x.y | member y of struct x | |
-> | x->y | member y of struct x | |
Ternary | ?: | x>y? x: y | if x>y then x else y |
note
This table is not an exhaustive list of all the operators in C, but rather a selection of the most commonly used operators.
Basic Example of operators in c
#include <stdio.h>
int main() {
int x = 5, y = 10, z = 15;
// Arithmetic operators
int sum = x + y; // sum = 15
int difference = y - x; // difference = 5
int multiplication = x * z; // multiplication = 75
int quotient = z / x; // quotient = 3
// Comparison operators
if (x < y) { // true
printf("x is less than y\n");
}
if (y >= z) { // false
printf("y is greater than or equal to z\n");
}
// Logical operator
if (x > 2 && y < 20) { // true
printf("x is greater than 2 and y is less than 20\n");
}
// Assignment operator
x = y; // x is now equal to 10
// Increment and Decrement operator
x++; // x is now equal to 11
y--; // y is now equal to 9
return 0;
}
Output:
Output:
x is less than y
x is greater than 2 and y is less than 20