Operators in C

Operators in C. Operators in C Tutorial. Learn Operators in C
Operators in C

What is Operators in C?

In C programming language, operators are symbols that perform various operations on operands such as variables, constants, and expressions. Operators are used to perform various tasks such as arithmetic operations, logical operations, comparison operations, and bit-wise operations.

Operators are used to manipulate data in a program and to perform various computations. C programming language supports a wide range of operators, including arithmetic operators, logical operators, comparison operators, assignment operators, bitwise operators, and conditional operators.

What is Operands in C?

An operand is a value or expression that is used as input to an operator. An operator is a symbol or keyword that performs a specific operation on one or more operands, such as addition, subtraction, multiplication, division, or comparison.

For example, in the expression a + b, a and b are operands and the + symbol is the operator. In the expression x > y, x and y are operands and the > symbol is the operator.

In C, operands can be of different data types such as integers, floating-point numbers, characters, pointers, or user-defined data types. The data type of an operand determines how it is stored in memory and how it can be used with operators.

Operands can also be constants or variables. A constant is a fixed value that cannot be changed during program execution, while a variable is a named storage location that can hold a value that can change during program execution.

Types of Operators in C:

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations on operands. These operators include:

Sr. No.Operator NameOperator SymbolInformation
1Addition+Adds two operands together.
2SubtractionSubtracts the second operand from the first.
3Multiplication*Multiplies two operands together.
4Division/Divides the first operand by the second.
5Modulus%Returns the remainder after division.

2. Logical Operators

Logical operators are used to perform logical operations on operands. These operators include:

Sr. No.Operator NameOperator SymbolInformation
1Logical AND&&Returns true if both operands are true.
2Logical OR||Returns true if either operand is true.
3Logical NOT!Returns the opposite of the operand.

3. Increment and Decrement Operators

Increment and Decrement operators are used to increase or decrease the value of a variable by one respectively.

Sr. No.Operator NameOperator SymbolInformation
1Increment++Increases the value of a variable by one.
2DecrementDecreases the value of a variable by one.

4. Comparison Operators

Comparison operators are used to compare two operands. These operators include:

Sr. No.Operator NameOperator SymbolInformation
1Equal to==Returns true if both operands are true.
2Not equal to!=Returns true if both operands are not equal.
3Greater than>Returns true if the first operand is greater than the second.
4Less than<Returns true if the first operand is less than the second.
5Greater than or equal to>=Returns true if the first operand is greater than or equal to the second.
6Less than or equal to<=Returns true if the first operand is less than or equal to the second.

5. Assignment Operators

Assignment operators are used to assign a value to a variable. These operators include:

Sr. No.Operator NameOperator SymbolInformation
1Assign=Assigns the value of the right operand to the left operand.
2Add and Assign+=Adds the value of the right operand to the left operand and assigns the result to the left operand.
3Subtract and Assign-=Subtracts the value of the right operand from the left operand and assigns the result to the left operand.
4Multiply and Assign*=Multiplies the value of the right operand by the left operand and assigns the result to the left operand.
5Divide and Assign/=Divides the left operand by the right operand and assigns the result to the left operand.
6Modulus and Assign%=Applies modulus operation on the left operand with the right operand and assigns the result to the left operand.

6. Bitwise Operators

Bitwise operators are used to perform bit-level operations on operands. These operators include:

Sr. No.Operator NameOperator SymbolInformation
1Bitwise AND&Performs a bitwise AND operation on the operands.
2Bitwise OR|Performs a bitwise OR operation on the operands.
3Bitwise NOT~Inverts the bits of the operand.
4Bitwise XOR^Performs a bitwise XOR (exclusive OR) operation on the operands.
5Left Shift<<Shifts the bits of the left operand to the left by the number of bits specified by the right operand.
6Right Shift>>Shifts the bits of the left operand to the right by the number of bits specified by the right operand.

7. Conditional Operator (Ternary Operator)

Conditional operator is also known as ternary operator. It is a shorthand way of writing an if-else statement and is used to make decisions based on a condition. It is used to evaluate a condition and return a value based on that condition. It takes three operands, and the syntax is as follows:

(condition ? expression1 : expression2)

Explanation:

The condition is a Boolean expression that evaluates to either true or false. If the condition is true, then the value of expression1 is returned, otherwise the value of expression2 is returned.

Example:

Here is an example that uses the conditional operator to determine the maximum of two numbers:

#include <stdio.h>

int main() {
   int a = 5, b = 10, max;
   max = (a > b) ? a : b;
   printf("The maximum of %d and %d is %d\n", a, b, max);
   return 0;
}

Output:

The maximum of 5 and 10 is 10

Explanation:

In this example, the condition a > b is evaluated first. If it is true, then the value of a is assigned to max, otherwise the value of b is assigned to max. The result is printed to the console.

Rules while using Operators in C:

  1. Operators should only be used with valid operands.
  2. The order of precedence of operators must be taken into account while using them in an expression.
  3. Operators should be used with compatible operands, otherwise, the behavior of the program may be undefined.
  4. The size of the operands should be compatible with the size of the operator being used.
  5. Division by zero should be avoided as it can lead to program crashes or undefined behavior.
  6. Operators that modify variables such as increment and decrement operators should be used with caution to avoid unintended side effects.
  7. Parentheses should be used to specify the order of evaluation in an expression, to avoid any ambiguity.
  8. It is recommended to use parentheses for readability and to avoid errors, even when the order of precedence is well-defined.

Modifications that can be made while using Operators in C:

There are various modifications that can be made while using operators. These modifications include:

  1. Operator Precedence: The order of evaluation of operators in an expression is determined by their precedence. Modifying the order of operators can change the result of the expression. Parentheses can be used to change the order of evaluation.
  2. Associativity: Associativity determines the direction of evaluation when multiple operators of the same precedence are used in an expression. Modifying the associativity can change the result of the expression.
  3. Operator Overloading: C does not support operator overloading, which means that the same operator cannot be used to perform different operations on different data types. However, some libraries provide their own implementations of operators to work with their custom data types.
  4. Operator Promotion: When an operator is used with operands of different data types, the compiler promotes the operands to a common type before performing the operation. Modifying the data types of operands can change the promoted type and the result of the operation.
  5. Operator Precision: When arithmetic operations are performed on floating-point numbers, the precision of the result can be affected by the data type used to represent the numbers. Modifying the data type of the operands or result can affect the precision of the operation.
  6. Short-Circuit Evaluation: Logical operators (&& and ||) use short-circuit evaluation, which means that the second operand may not be evaluated if the result can be determined from the first operand. Modifying the order of operands can affect the performance of the evaluation.

More: