# Operators and Expressions

Operators in C are symbols or keywords that perform various operations on operands (variables, constants, or expressions). They are used to manipulate data, perform calculations, compare values, and control program flow. C provides a wide range of operators categorized into different types.

Arithmetic Operators

Assignment Operators

Relational Operators

Logical Operators

Bitwise Operators

Increment and Decrement Operators

Arithmetic Operators:

+ Addition: Adds two operands together. Example: int sum = 10 + 5;

- Subtraction: Subtracts one operand from another. Example: int difference = 10 - 5;

* Multiplication: Multiplies two operands. Example: int product = 10 * 5;

/ Division: Divides one operand by another. Example: int quotient = 10 / 5;

% Modulus: Returns the remainder after division. Example: int remainder = 10 % 3

Assignment Operators:

= Assignment: Assigns a value to a variable. Example: int x = 10;

+= Addition assignment: Adds a value to a variable and assigns the result to the variable. Example: x += 5; (equivalent to x = x + 5;)

-= Subtraction assignment: Subtracts a value from a variable and assigns the result to the variable. Example: x -= 5; (equivalent to x = x - 5;)

*= Multiplication assignment: Multiplies a variable by a value and assigns the result to the variable. Example: x *= 5; (equivalent to x = x * 5;)

/= Division assignment: Divides a variable by a value and assigns the result to the variable. Example: x /= 5; (equivalent to x = x / 5;)

%= Modulus assignment: Performs modulus operation on a variable and a value, and assigns the result to the variable. Example: x %= 3; (equivalent to x = x % 3;)

Relational Operators:

== Equal to: Checks if two operands are equal. Example: if (x == y) { ... }

!= Not equal to: Checks if two operands are not equal. Example: if (x != y) { ... }

< Less than: Checks if the left operand is less than the right operand. Example: if (x < y) { ... }

> Greater than: Checks if the left operand is greater than the right operand. Example: if (x > y) { ... }

<= Less than or equal to: Checks if the left operand is less than or equal to the right operand. Example: if (x <= y) { ... }

>= Greater than or equal to: Checks if the left operand is greater than or equal to the right operand. Example: if (x >= y) { ... }

Logical Operators:

&& Logical AND: Returns true if both operands are true. Example: if (x > 0 && y > 0) { ... }

|| Logical OR: Returns true if at least one of the operands is true. Example: if (x > 0 || y > 0) { ... }

! Logical NOT: Returns the opposite of the operand's truth value. Example: if (!(x > 0)) { ... }

Bitwise Operators:

Bitwise AND (&):

Performs a bitwise AND operation between the corresponding bits of two operands.

If both bits are 1, the result bit is 1; otherwise, it is 0.

Example: result = a & b;

Bitwise OR (|)

Performs a bitwise OR operation between the corresponding bits of two operands.

If at least one bit is 1, the result bit is 1; otherwise, it is 0.

Example: result = a | b;

Bitwise XOR (^):

Performs a bitwise XOR (exclusive OR) operation between the corresponding bits of two operands.

If the two bits are different (one is 0 and the other is 1), the result bit is 1; otherwise, it is 0.

Example: result = a ^ b;

Bitwise NOT (~):

Performs a bitwise NOT operation on a single operand, inverting each bit.

If the bit is 0, it becomes 1, and if the bit is 1, it becomes 0.

Example: result = ~a;

Left Shift (<<):

Shifts the bits of the left operand to the left by a specified number of positions.

Zeros are shifted in from the right, and the leftmost bits are discarded.

Example: result = a << n;

Right Shift (>>):

Shifts the bits of the left operand to the right by a specified number of positions.

For unsigned types, zeros are shifted in from the left.

For signed types, the sign bit is replicated to maintain the sign of the number.

Example: result = a >> n;

Increment and Decrement Operators:

++ Increment: Increases the value of a variable by 1. Example: x++; (equivalent to x = x + 1;)

-- Decrement: Decreases the value of a variable by 1. Example: x--; (equivalent to x = x - 1;)