# Operators in C

Embarking on a journey through the realm of C programming, this article unveils the intricate world of operators. These powerful symbols enable programmers to manipulate data, execute calculations, and make logical decisions. As we delve into the depths of operator categories and functionalities, you’ll discover how to harness their potential to craft efficient and elegant code.

## Types of Operators

Operators in C are the bedrock of programming, facilitating various tasks.

**Let’s dive into the distinctive characteristics of each operator type:**

#### 1. Arithmetic Operators

These operators bring mathematical prowess to your code, allowing you to perform basic arithmetic operations.

**Addition (+):**Combines two operands to yield their sum.**Subtraction (-):**This symbol performs the action of deducting the value of the right operand from the value of the left operand.**Multiplication (*):**Multiplies two operands, producing their product.**Division (/):**The division operator performs the operation of dividing the value of the left operand by the value of the right operand.

Operator | Operation | Example |

+ | Addition | X + Y = 42 |

– | Subtraction | X – Y = -18 |

* | Multiplication | X * Y = 315 |

/ | Division | Y / X = 7 |

% | Modulus (Remainder) | Y % X = 0 |

++ | Increment (Increase by One) | X++ = 8 |

— | Decrement (Decrease by One) | X– = 6 |

#### 2. Relational Operators

**Relational operators empower you to make comparisons and evaluate relationships between values.**

**Equal to (==):**Evaluates if two operands possess equality.**Not equal to (!=):**Assesses whether two operands lack equality.**Less than (<):**Verifies if the left operand is of lesser value than the right operand.**Greater than (>):**Establishes whether the left operand holds a greater value than the right operand.

Operator | Name | Usage |

== | Equality | Verifies if the values of two operands are the same. |

!= | Inequality | Determines if the values of two operand variables or constants differ. |

<= | Less than or Equal to | Verifies if one value is less than or equal to another. |

>= | Greater than or Equal to | Establishes if one value is greater than or equal to another. |

< | Less than | Compares whether one operand is smaller than the other. |

> | Greater than | Compares whether one operand is larger than the other. |

#### 3. Logical Operators

Logical operators help make sense of data by performing logical operations and making informed decisions.

**Logical AND (&&):**Produces a true outcome only when both operands are true.**Logical OR (||):**When used, this operator returns a true value if at least one of the operands is true.**Logical NOT (!):**Negates the truth value of an operand.

Operator | Description | Example |

&& | Logical AND operator. Assesses as true when both operands are in a true state. | (X && Y) is false. |

|| | Logical OR Operator. Turns true when at least a single operand holds a true value. | (X || Y) is true. |

! | Logical NOT Operator. Inverts the truth value of its operand. If true, it becomes false. | !(X && Y) is true. |

#### 4. Assignment Operators

Assignment operators serve as tools to assign values to variables efficiently.

**Assignment (=):**When utilizing this operator, the value on the right operand gets assigned to the left operand.**Add and Assign (+=):**Sum the right operand with the left operand and subsequently assign the resultant value.**Subtract and Assign (-=):**With this operator, the value of the right operand is subtracted from the value of the left operand, and the outcome is assigned.**Multiply and Assign (*=):**Utilizing this operator, the left operand is multiplied by the right operand, and the product is assigned to the left operand.

Operator | Description | Example |

= | Simple assignment operator. Assigns the value present on the right to the left counterpart. | Result = A + B assigns the A + B value to Result. |

+= | Add AND assignment operator. Adds the right operand to the left operand and assigns the outcome. | Total += A is the same as Total = Total + A |

-= | Subtract AND assignment operator. Subtracting the value of the right operand from the left, it then allocates the resulting value. | Count -= A is the same as Count = Count – A |

*= | Multiply AND assignment operator. Multiplies the value of the left operand by the value of the right operand, then assigns the resulting product. | Product *= A is the same as Product = Product * A |

/= | Divide AND assignment operator. Divides the value of the left operand by the value of the right operand and then assigns the quotient as the result. | Amount /= A is the same as Amount = Amount / A |

%= | Modulus AND assignment operator. Computes the modulus using two operands and assigns the outcome as the result. | Remainder %= A is the same as Remainder = Remainder % A |

<<= | Left shift AND assignment operator. Shifts bits left by the specified number of positions and assigns the result. | Data <<= 2 is the same as Data = Data << 2 |

>>= | Right shift AND assignment operator. Shifts bits right by the specified number of positions and assigns the result. | Value >>= 2 is the same as Value = Value >> 2 |

&= | Bitwise AND assignment operator. Executes a bitwise AND operation and assigns the resulting value. | Mask &= 2 is the same as Mask = Mask & 2 |

^= | Bitwise exclusive OR assignment operator. Conducts a bitwise exclusive OR operation and designates the outcome for assignment. | Flags ^= 2 is the same as Flags = Flags ^ 2 |

|= | Bitwise inclusive OR assignment operator. Carries out a bitwise XOR operation and assigns the resultant value. | Options |= 2 is the same as Options = Options | 2 |

#### 5. Bitwise Operators

Bitwise operators operate on individual bits, offering a level of precision for various operations.

**Bitwise AND (&):**Performs a bitwise AND operation between corresponding bits.**Bitwise OR (|):**Executes a bitwise OR operation between corresponding bits.**Bitwise XOR (^):**Conducts a bitwise exclusive OR operation between corresponding bits.**Bitwise NOT (~):**Inverts the bits of the operand.

Operator | Description | Example |

& | The binary AND Operator replicates a bit to the output if it exists in both operands. | (X & Y) = 12, i.e., 1100 |

| | Binary OR Operator copies a bit if present in either operand. | (X | Y) = 61, i.e., 111101 |

^ | Binary XOR Operator copies a bit if set in one operand but not both. | (X ^ Y) = 49, i.e., 110001 |

~ | Binary One’s Complement Operator is unary and inverts all the bits. | (~X) = -11, i.e., -00001101 |

<< | The Binary Left Shift Operator shifts the bits of the left operand by the specified number of places. | (X << 2) = 240, i.e., 11110000 |

>> | Binary Right Shift Operator shifts the bits of the left operand right by the specified number of places. | (X >> 2) = 15, i.e., 00001111 |

#### 6. Unary Operators

Unary operators apply to a single operand, often used for incrementing or decrementing.

**Unary Plus (+):**Represents the positive value of an operand.**Unary Minus (-):**Indicates the negative value of an operand.**Increment (++):**This operator increases the value of an operand by 1.**Decrement (–):**When this operator is applied, the value of an operand is decreased by 1.

#### 7. Conditional Operator

The conditional operator offers a concise way to make decisions based on a condition’s outcome.

**Syntax:** condition? value_if_true : value_if_false.

**Example:** result = (score > passing_score) ? “Pass”: “Fail”.

### Other Operators

In addition to the familiar operators covered in the previous sections, the world of C programming offers a collection of other operators designed for specific tasks. These operators might not be as commonly encountered, but they are nonetheless important tools in a programmer’s toolkit. Let’s delve into some of these lesser-known operators and understand their functions.

#### sizeof Operator

The sizeof the operator holds a pivotal role in the realm of C programming. It operates at compile-time and serves to determine the size, in bytes, of a particular data type or variable. When applied to a data type or variable, it returns the size as an unsigned integral value, typically represented using size_t. For instance, sizeof(int) would yield the size of an integer in bytes. This operator proves particularly useful when allocating memory dynamically, ensuring that the appropriate amount of memory is reserved. To grasp the finer details of the sizeof operator’s usage, refer to the dedicated resources available on this topic.

#### Comma Operator

The comma operator might seem unassuming, but it has its own unique role in C programming. Denoted by the token, it’s a binary operator that carries out its operations in a sequence. It evaluates the expression on its left, discards the result, and then evaluates the expression on its right, returning that value. While this operator has the lowest precedence in C, it can be surprisingly handy in certain situations. It acts both as an operator and a separator, showcasing its versatility within the language. For further insights into the comma operator’s nuances, explore the dedicated articles discussing its functionalities.

#### Conditional Operator

The conditional operator, also known as the ternary operator, provides a concise way to make decisions based on a condition’s outcome. It takes the form Expression1. Expression2 : Expression3. If Expression1 evaluates to true, Expression2 is executed, and its result is returned. Otherwise, if Expression1 is false, Expression3 is executed, and its result is returned. This operator offers a compact alternative to using if…else statements for certain scenarios, enhancing code readability and conciseness. Delve into specific resources on the conditional operator for a deeper understanding of its practical applications.

#### Dot (.) and Arrow (->) Operators

When dealing with classes, structures, and unions, the dot and arrow operators come into play. The dot operator (.) is used when working with the actual object, while the arrow operator (->) is employed when dealing with a pointer to an object. These operators enable you to access individual members of these complex data types, allowing you to manipulate and interact with the data they hold. To explore the intricacies of using these operators effectively, refer to specialized guides on both the dot operator and the arrow operator.

#### Cast Operator

Casting is the process of converting one data type to another, and the cast operator facilitates this conversion. For instance, using the cast operator, you can convert a floating-point number to an integer, truncating the decimal part. The cast operator takes the form (type) expression, where the expression is the value you wish to cast, and (type) specifies the desired target data type. Mastering casting is essential when dealing with mixed data types or when you need to ensure compatibility between different parts of your code. For a more comprehensive understanding of the cast operator, consult resources dedicated to this subject.

#### & and * Operators

Commonly known as the address-of operator, the & operator retrieves the memory address of a variable. For example, &a would yield the address of the variable a. On the other hand, the * operator, known as the pointer operator, creates a pointer to a variable. Using *var as an example, it would refer to a variable named var. These operators are essential when working with pointers, a powerful concept in C that allows you to manipulate memory addresses directly. To dive deeper into the workings of these operators and pointers, consult specialized articles that unravel their intricacies.

Operator | Description | Example |

sizeof() | Returns the size of a variable. | When used with an integer like ‘a’, the sizeof(a) operation will yield 4 as the outcome. |

& | Returns the address of a variable. | The expression &a; provides the factual memory location of the variable. |

* | Pointer to a variable. | *a; |

? : | Conditional Expression. | If Condition is true? then value X: Otherwise value Y |

### Conclusion

Unveiling the diversity and significance of operators, we’ve explored how each category contributes to the foundation of C programming. From arithmetic operators that perform fundamental calculations to bitwise operators that manipulate individual bits and from logical operators that guide decision-making to unary operators that streamline incrementing and decrementing, each type plays a vital role. Armed with this knowledge, you’re ready to wield operators as your programming allies, crafting code that’s efficient, logical, and powerful.

**Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google **