C Operators
Operators in C
Special symbols that are used to perform actions or operations are known as operators. They could be both unary or binary. For example, the symbol asterisk (*) is used to perform multiplication in C so it is an operator and it is a binary operator. This section covers all types of operators.
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations such as addition, subtraction, etc. A few of the simple arithmetic operators are:
Operator | Description |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus |
Let’s see their implementation in C.
#include <stdio.h>
int main() {
int a = 2;
int b = 3;
printf("a + b = %d\n", a + b);
}
Output:
a + b = 5
Relational Operators
Relational operators are used for the comparison between two or more numbers or even expressions. C has six relational operators:
Operator | Description |
---|---|
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
== | Is equal to |
!= | Is not equal to |
Let’s see their implementation in C.
#include <stdio.h>
int main() {
int a = 2;
int b = 3;
printf("a == b = %d\n", a == b);
}
Output:
a == b = 0
Logical Operators
There are three logical operators i.e. AND, OR, and NOT. They can be used to compare Boolean values but are mostly used to compare conditions to see whether they are satisfying or not.
Operator | Description |
---|---|
&& | AND Operator |
|| | OR Operator |
! | NOT Operator |
Let’s see their implementation in C.
#include <stdio.h>
int main() {
int a = 1;
int b = 0;
printf("a or b = %d\n", a || b);
}
Output:
a or b = 1
Bitwise Operators
A bitwise operator is used to performing operations at the bit level. To obtain the results, they convert our input values into binary format and then process them using whatever operator they are being used with.
Operator | Description |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise Complement |
>> | Shift Right Operator |
<< | Shift Left Operator |
Let’s see their implementation in C.
#include <stdio.h>
int main() {
int a = 2; // 10
int b = 3; // 11
printf("a xor b = %d\n", a ^ b);
}
Output:
a xor b = 1
Assignment Operators
Assignment operators are used to assign values. We will use them in almost every program we develop.
int a = 0;
int b = 1;
Equal to (=) is the assignment operator here. It is assigning 0 to a and 1 to b in the above example.
Operator | Description |
---|---|
= | It assigns the right side operand value to the left side operand. |
+= | It adds the right operand to the left operand and assigns the result to the left operand. |
-= | It subtracts the right operand from the left operand and assigns the result to the left operand. |
*= | It multiplies the right operand with the left operand and assigns the result to the left operand. |
/= | It divides the left operand with the right operand and assigns the result to the left operand. |