C Operators
Operators are signs that act upon operands.
Arithmetic Operators
Arithmetic operators are utilized to carry out mathematical operations like addition, subtraction, multiplication, division, and modulus.
Operator | Description | Syntax |
---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus | a % b |
Example:
#include
<stdio.h>
int
main
()
{
int
a
=
5;
int
b
=
3;
printf
("a + b = %d\n", a + b);
printf
("a - b = %d\n", a - b);
printf
("a * b = %d\n", a * b);
printf
("a / b = %d\n", a / b);
printf
("a % b = %d\n", a % b);
return
0;
}
Output:
a + b = 8
a - b = 2
a * b = 15
a / b = 1.6
a % b = 0.15
Relational Operators
Relational operators are applied for comparing the relation between two operands.
Operator | Description | Syntax |
---|---|---|
> | Greater than | a > b |
<< /td> | Less than | a < b |
>= | Greater than or equal to | a >= b |
=<< /td> | Less than or equal to | a <= b |
== | Equal to | a == b |
!= | Not equal to | a != b |
Example:
#include
<stdio.h>
int
main
()
{
int
a
=
5;
int
b
=
3;
printf
("a < b : %d\n", a < b);
printf
("a > b : %d\n", a > b);
printf
("a == b = %d\n", a == b);
printf
("a != b : %d\n", a != b);
return
0;
}
Output:
a < b : 0
a > b : 1
a == b = 0
a != b : 1
Logical Operators
Logical operators are employed to carry out logical operations between two or more conditions.
Operator | Description | Syntax |
---|---|---|
&& | AND Operator | a && b |
|| | OR Operator | a || b |
! | NOT Operator | !a |
Example:
#include
<stdio.h>
int
main
()
{
int
a
=
15;
int
b
=
6;
printf
("a && b : %d\n", a && b);
printf
("a || b = %d\n", a || b);
printf
("!a: %d\n", !a);
return
0;
}
Output:
a && b : 1
a || b = 1
!a: 0
Assignment Operators
Assignment operators are applied to assign values to a variable.
Operator | Description | Syntax |
---|---|---|
= | It places the right side operand value into the left side operand. | a = b |
+= | It adds the right operand to the left operand and stores the result in the left operand. | a += b |
-= | It takes the right operand away from the left operand and places the result in the left operand. | a -= b |
*= | It multiplies the left operand by the right operand and puts the result into the left operand. | a *= b |
/= | It performs division of the left operand by the right operand and stores the result in the left operand. | a /= b |
Example:
#include
<stdio.h>
int
main
()
{
int
a
=
15;
int
b
=
6;
printf
("a = b = %d\n", a = b);
printf
("a += b = %d\n", a += b);
printf
("a -= b = %d\n", a -= b);
printf
("a *= b = %d\n", a *= b);
printf
("a /= b = %d\n", a /= b);
return
0;
}
Output:
a = b = 6
a += b = 12
a -= b = 6
a *= b = 36
a /= b = 6
Bitwise Operators
Bitwise operators operate at the bit level.
Operator | Description | Syntax |
---|---|---|
& | Bitwise AND | a & b |
| | Bitwise OR | a | b |
^ | Bitwise XOR | a ^ b |
~ | Bitwise Complement | ~a |
>> | Shift Right Operator | a >> b |
<<< /td> | Shift Left Operator | a << b |
Example:
#include
<stdio.h>
int
main
()
{
int
a
=
15;
int
b
=
6;
printf
("a & b: %d\n", a & b);
printf
("a | b: %d\n", a | b);
printf
("a ^ b: %d\n", a ^ b);
printf
("~a: %d\n", ~a);
printf
("a >> b: %d\n", a >> b);
printf
("a << b: %d\n", a << b);
return
0;
}
Output:
a & b: 6
a | b: 15
a ^ b: 9
~a: -16
a >> b: 0
a << b: 960