# Operators in C

Operators in C are special symbols that perform specific operations on one, two, or three operands, and then return a result. C supports a wide variety of operators, including arithmetic, relational, logical, bitwise, assignment, and others. In this article, we will discuss the different types of operators in C and provide examples of how to use them in your code.

## Operators in C

### 1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations, such as addition, subtraction, multiplication, and division.

Examples:

• Subtraction (-)
• Multiplication (*)
• Division (/)
• Modulus (%)
• Increment (++)
• Decrement (–)

For example, the following code demonstrates the use of arithmetic operators:

int a = 10, b = 5;

int c = a + b; // c = 15

int d = ab; // d = 5

int e = a * b; // e = 50

int f = a / b; // f = 2

int g = a % b; // g = 0

a++; // a = 11

b–; // b = 4

### 2. Relational Operators

Relational operators are used to compare two operands and return a Boolean value indicating whether the comparison is true or false.

Examples:

• Less than (<)
• Greater than (>)
• Less than or equal to (<=)
• Greater than or equal to (>=)
• Equal to (==)
• Not equal to (!=)

For example, the following code demonstrates the use of relational operators:

int a = 10, b = 5;

bool c = a < b; // c = false

bool d = a > b; // d = true

bool e = a <= b; // e = false

bool f = a >= b; // f = true

bool g = a == b; // g = false

bool h = a != b; // h = true

### 3. Logical Operators

Logical operators are used to perform logical operations, such as AND, OR, and NOT.

Examples:

• Logical AND (&&)
• Logical OR (||)
• Logical NOT (!)

For example, the following code demonstrates the use of logical operators:

bool a = true, b = false;

bool c = a && b; // c = false

bool d = a || b; // d = true

bool e = !a; // e = false

### 4. Bitwise Operators

Bitwise operators are used to perform bitwise operations, such as AND, OR, NOT, and XOR.

Examples:

• Bitwise AND (&)
• Bitwise OR (|)
• Bitwise NOT (~)
• Bitwise XOR (^)
• Left shift (<<)
• Right shift (>>)

For example, the following code demonstrates the use of bitwise operators:

int a = 10, b = 5;

int c = a & b; // c = 0

int d = a | b; // d = 15

int e = ~a; // e = -11

int f = a ^ b; // f = 15

int g = a << 2; // g = 40

int h = a >> 2; // h = 2

### 5. Assignment Operators

Assignment operators are used to assign a value to a variable. The most basic assignment operator is the equal sign (=), which assigns the value on the right to the variable on the left.

Examples:

• Basic assignment (=)
• Subtraction assignment (-=)
• Multiplication assignment (*=)
• Division assignment (/=)
• Modulus assignment (%=)
• Left shift assignment (<<=)
• Right shift assignment (>>=)
• Bitwise AND assignment (&=)
• Bitwise OR assignment (|=)
• Bitwise XOR assignment (^=)

For example, the following code demonstrates the use of assignment operators:

int a = 10;

a = 5; // a is now 5

a += 5; // a is now 10

a -= 2; // a is now 8

a *= 2; // a is now 16

a /= 4; // a is now 4

a %= 3; // a is now 1

a <<= 2; // a is now 4

a >>= 1; // a is now 2

a &= 3; // a is now 2

a |= 1; // a is now 3

a ^= 2; // a is now 1

### 6. Conditional (ternary) operator

The conditional operator is also known as the ternary operator. It is represented by the ? symbol. It is used to perform a simple if-else condition in a single line.

Syntax: condition ? if_true_value : if_false_value;

Example:

int x = 10;

int y = 20;

int max = (x>y) ? x : y; // max = 20

### 7. sizeof() operator

The sizeof operator returns the size of a variable or data type in bytes.

Example:

int x = 10;

size_t size = sizeof(x); // size = 4

### Conclusion

In conclusion, operators in C are special symbols that perform specific operations on one, two, or three operands, and then return a result.

C supports a wide variety of operators, including arithmetic, relational, logical, bitwise, assignment, and others. Understanding how to use operators in C can make your code more efficient and easier to read and understand.

It’s important to note that in C and C++, the order of operations follows the order of precedence and associativity of the operators. Be sure to understand the order of precedence and associativity of the operators and use parenthesis when necessary to ensure the code produces the expected results.