C Operators
There are classified into several categories
- Arithmetic Operators - +, -, *, /, %
- Relational Operators - ==, !=, <, >, <=, >=
- Logical Operators - &&, !!, !
- Assignment Operators - =, +=, -=, *=, /=, %/
- Increment and Decrement Operators - ++, --
- Bitwise Operators - &, |, ^, ~, <<, >>
- Conditional (Ternary) Operator - ? :
- Comma Operator - ,
Arithmetic Operators
Operator
Description
Example
+
Addition
int sum = a + b;
-
Subtraction
int diff = a - b;
*
Multiplication
int product = a * b;
/
Division
int quotient = a / b;
%
Modulus (remainder)
int remainder = a % b;
Example
#include <stdio.h>
int main() {
int a = 5, b = 2;
int sum = a + b;
int diff = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
return 0;
}
Relational Operators
Operator
Description
Example
==
Equal to
int isEqual = (a == b);
!=
Not equal to
int isNotEqual = (a != b);
>
Greater than
int isGreaterThan = (a > b);
<
Less than
int isLessThan = (a < b);
>=
Greater than or equal to
int isGreaterOrEqual = (a >= b);
<=
Less than or equal to
int isLessOrEqual = (a <= b);
Example
#include <stdio.h>
int main() {
int a = 5, b = 2;
int isEqual = (a == b);
int isNotEqual = (a != b);
int isGreaterThan = (a > b);
int isLessThan = (a < b);
int isGreaterOrEqual = (a >= b);
int isLessOrEqual = (a <= b);
return 0;
}
Logical Operators
Operator
Description
Example
&&
Logical AND
int logicalAnd = (x && y);
||
Logical OR
int logicalOr = (x || y);
!
Logical NOT
int logicalNot = !x;
Example
#include <stdio.h>
int main() {
int x = 1, y = 0;
int logicalAnd = (x && y);
int logicalOr = (x || y);
int logicalNot = !x;
return 0;
}
Assignment, Increment and Decrement Operators
Assignment Operators are used to assign values to variables.
Increment and Decrement Operators used to increase or decrease the value of a variable by 1.
Operator
Description
Example
=
Assignment
a = 5;
+=
Addition assignment
a += 3;
-=
Subtraction assignment
a -= 2;
*=
Multiplication assignment
a *= 4;
/=
Division assignment
a /= 5;
%=
Modulus assignment
a %= 5;;
++
Increment
a++;
--
Decrement
a--;
Example
#include <stdio.h>
int main() {
int a = 5;
a += 3;
printf(a);
a -= 2;
printf(a);
a *= 4;
printf(a);
a /= 2;
printf(a);
a %= 3;
printf(a);
printf(a++);
print(a--);
return 0;
}
Bitwise Operators
Used for manipulation of individual bits.
Operator
Description
Example
&
Bitwise AND
int bitwiseAnd = a & b;
|
Bitwise OR
int bitwiseAnd = a | b;
^
Bitwise XOR
int bitwiseXor = a ^ b;
~
Bitwise NOT
int bitwiseNot = ~a;
<<
Left shift
int leftShift = a << 1;
>>
Right shift
int rightShift = a >> 1;
Example
#include <stdio.h>
int main() {
int a = 5;
int bitwiseAnd = a & b;
int bitwiseOr = a | b;
int bitwiseXor = a ^ b;
int bitwiseNot = ~a;
int leftShift = a << 1;
int rightShift = a >> 1;
return 0;
}
Conditional (Ternary) and Comma Operators
Comma Operator allows multiple expressions to be evaluated in a single statement.
Operator
Description
Example
? :
Ternary conditional (shorthand for if-else statement)
int max = (a > b) ? a : b;
,
Evaluates multiple expressions sequentially
int result = (a++, b++, c++);
Learn about them in C Booleans