C Operators

Operators in C are symbols used to perform various operations on variables and values. They are classified based on the type of operation they perform.

Types of Operators

1. Arithmetic Operators: Used for basic arithmetic operations.

+ (Addition): Adds two values.

- (Subtraction): Subtracts one value from another.

* (Multiplication): Multiplies two values.

/ (Division): Divides one value by another.

% (Modulus): Gives the remainder of a division.

Example:

int sum = 5 + 3; // 8
int diff = 5 - 3; // 2
int prod = 5 * 3; // 15
int quot = 5 / 3; // 1 (integer division)
int rem = 5 % 3; // 2

2. Relational Operators: Compare two values.


== (Equal to): Checks if two values are equal.

!= (Not equal to): Checks if two values are not equal.

> (Greater than): Checks if the left value is greater than the right value.

< (Less than): Checks if the left value is less than the right value.

>= (Greater than or equal to): Checks if the left value is greater than or equal to the right value.

<= (Less than or equal to): Checks if the left value is less than or equal to the right value.

Example:

int a = 5, b = 3;
int isEqual = (a == b); // 0 (false)
int isGreater = (a > b); // 1 (true)

3. Shift Operators: Shift bits to the left or right.


<< (Left shift): Shifts bits to the left.

>> (Right shift): Shifts bits to the right.

Example:

int x = 5; // 0000 0101 in binary
int y = x << 1; // 0000 1010 in binary (10 in decimal)

4. Logical Operators: Perform logical operations.


&& (Logical AND): Returns true if both operands are true.

|| (Logical OR): Returns true if at least one operand is true.

! (Logical NOT): Inverts the truth value.

Example:

int a = 1, b = 0;
int resultAnd = (a && b); // 0 (false)
int resultOr = (a || b); // 1 (true)
int resultNot = !a; // 0 (false)

5. Bitwise Operators: Perform operations on bits.


& (Bitwise AND): Performs AND operation on each bit.

| (Bitwise OR): Performs OR operation on each bit.

^ (Bitwise XOR): Performs XOR operation on each bit.

~ (Bitwise NOT): Inverts each bit.

Example:

int a = 5; // 0000 0101 in binary
int b = 3; // 0000 0011 in binary
int resultAnd = a & b; // 0000 0001 (1 in decimal)
int resultOr = a | b; // 0000 0111 (7 in decimal)

6. Ternary or Conditional Operator: A shorthand for if-else statements.


? : (Ternary): Returns one of two values based on a condition.

Example:

nt a = 5, b = 10;
int max = (a > b) ? a : b; // 10

7. Assignment Operators: Used to assign values to variables.


= (Assignment): Assigns value to a variable.

+= (Add and assign): Adds value and assigns the result.

-= (Subtract and assign): Subtracts value and assigns the result.

*= (Multiply and assign): Multiplies value and assigns the result.

/= (Divide and assign): Divides value and assigns the result.

%= (Modulus and assign): Applies modulus and assigns the result.

Example:

int a = 5;
a += 3; // a = 8
a -= 2; // a = 6

8. Miscellaneous Operators: Other operators with special purposes.


, (Comma): Separates multiple expressions.

. (Dot): Accesses a member of a structure.

-> (Arrow): Accesses a member of a structure through a pointer.

Example:

struct Person {
int age;
};
struct Person p;
p.age = 30; // Use dot operator to access member

Operator Precedence and Associativity

Operator precedence determines which operator is evaluated first in an expression. Associativity determines the order of evaluation for operators of the same precedence.

Operator Precedence Table:

Category Operator Associativity
Postfix () [] -> . ++ -- Left to right
Unary + - ! ~ ++ -- (type)* & sizeof Right to left
Multiplicative * / % Left to right
Additive + - Left to right
Shift <<>> Left to right
Relational < <=> >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %= >>= <<= &=^=|= Right to left
Comma , Left to right

Example:

int value = 10 + 20 * 2; // Multiplier (*) is evaluated before addition (+)

In this example, 20 * 2 is evaluated first, resulting in 40, then 10 is added to 40, giving a final value of 50.