C Operators

Operators are symbols that perform operations on variables and values. Let's explore the main types:

1. Arithmetic Operators

Used for basic mathematical operations.

OperatorDescriptionExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulus (remainder)a % b
int a = 10, b = 3;
printf("%d", a + b);  // Output: 13

2. Relational Operators

Used to compare two values. Returns 1 if true, 0 if false.

OperatorDescriptionExample
==Equal toa == b
!=Not equal toa != b
>Greater thana > b
<Less thana < b
>=Greater than or equal toa >= b
<=Less than or equal toa <= b
int a = 5, b = 10;
printf("%d", a < b);  // Output: 1 (true)

3. Logical Operators

Used to combine conditional expressions.

OperatorDescriptionExample
&&Logical AND(a > 0) && (b > 0)
||Logical OR(a > 0) || (b > 0)
!Logical NOT!(a > 0)
int a = 5, b = -1;
printf("%d", (a > 0) && (b > 0));  // Output: 0 (false)

4. Bitwise Operators

Know more in Details

Used to perform operations on bits.

OperatorDescriptionExample
&Bitwise ANDa & b
|Bitwise ORa | b
^Bitwise XORa ^ b
~Bitwise NOT~a
<<Left Shifta << 2
>>Right Shifta >> 2
int a = 5, b = 3;
printf("%d", a & b);  // Output: 1 (0101 & 0011 = 0001)