Table of Contents
There are 7 most commonly used operators in C that play a vital role in programming.
Arithmetic operators:
Arithmetic operators are a type of operator that perform mathematical operations on numerical values. In programming, arithmetic operators are used extensively in mathematical calculations, from simple addition and subtraction to more complex calculations involving multiple operations. The most common arithmetic operators are:
1. Addition (+): The addition operator is used to add two or more values together. For example, if we have two variables `a` and `b` with values 5 and 3 respectively, we can add them together using the addition operator as follows:
“`
a = 5
b = 3
c = a + b
“`
The value of `c` will be 8.
2. Subtraction (-): The subtraction operator is used to subtract one value from another. For example, if we have two variables `a` and `b` with values 5 and 3 respectively, we can subtract `b` from `a` using the subtraction operator as follows:
“`
a = 5
b = 3
c = a – b
“`
The value of `c` will be 2.
3. Multiplication (*): The multiplication operator is used to multiply two or more values together. For example, if we have two variables `a` and `b` with values 5 and 3 respectively, we can multiply them using the multiplication operator as follows:
“`
a = 5
b = 3
c = a * b
“`
The value of `c` will be 15.
4. Division (/): The division operator is used to divide one value by another. For example, if we have two variables `a` and `b` with values 6 and 3 respectively, we can divide `a` by `b` using the division operator as follows:
“`
a = 6
b = 3
c = a / b
“`
The value of `c` will be 2.
5. Modulus (%): The modulus operator is used to find the remainder when one value is divided by another. For example, if we have two variables `a` and `b` with values 5 and 3 respectively, we can find the remainder when `a` is divided by `b` using the modulus operator as follows:
“`
a = 5
b = 3
c = a % b
“`
The value of `c` will be 2, which is the remainder when 5 is divided by 3.
6. Exponentiation (**): The exponentiation operator is used to raise one value to the power of another. For example, if we have two variables `a` and `b` with values 2 and 3 respectively, we can raise `a` to the power of `b` using the exponentiation operator as follows:
“`
a = 2
b = 3
c = a ** b
“`
The value of `c` will be 8, which is 2 raised to the power of 3.
These are the basic arithmetic operators in programming, and they can be combined in different ways to perform complex calculations. The order of operations matters in mathematical calculations, and parentheses can be used to control the order of operations.
Assignment operators:
Assignment operators in programming are used to assign a value or an expression to a variable. They combine the assignment operator ‘=’ with arithmetic, bitwise, or logical operators, to perform the operation and then assign the result to a variable.
Here are the commonly used assignment operators:
1. “=” (Simple Assignment Operator)
The simple assignment operator is used to assign a value to a variable. For example, if we want to assign a value of 10 to a variable x, we can use the following code:
“`
x = 10;
“`
2. “+=” (Addition Assignment Operator)
The addition assignment operator adds the right-hand side value to the left-hand side variable and assigns the result to the left-hand side variable. For example, if we want to add 5 to a variable x and then assign the result to x, we can use the following code:
“`
x += 5;
“`
This is equivalent to writing x = x + 5.
3. “-=” (Subtraction Assignment Operator)
The subtraction assignment operator subtracts the right-hand side value from the left-hand side variable and assigns the result to the left-hand side variable. For example, if we want to subtract 3 from a variable x and then assign the result to x, we can use the following code:
“`
x -= 3;
“`
This is equivalent to writing x = x – 3.
4. “*=” (Multiplication Assignment Operator)
The multiplication assignment operator multiplies the right-hand side value with the left-hand side variable and assigns the result to the left-hand side variable. For example, if we want to multiply a variable x by 4 and then assign the result to x, we can use the following code:
“`
x *= 4;
“`
This is equivalent to writing x = x * 4.
5. “/=” (Division Assignment Operator)
The division assignment operator divides the left-hand side variable by the right-hand side value and assigns the result to the left-hand side variable. For example, if we want to divide a variable x by 2 and then assign the result to x, we can use the following code:
“`
x /= 2;
“`
This is equivalent to writing x = x / 2.
6. “%=” (Modulus Assignment Operator)
The modulus assignment operator calculates the remainder of the left-hand side variable divided by the right-hand side value and assigns the result to the left-hand side variable. For example, if we want to find the remainder when a variable x is divided by 3 and then assign the result to x, we can use the following code:
“`
x %= 3;
“`
This is equivalent to writing x = x % 3.
Here is an example of how to use the assignment operators:
“`
int x = 10;
x += 5; // x = x + 5 = 15
x -= 3; // x = x – 3 = 12
x *= 4; // x = x * 4 = 48
x /= 2; // x = x / 2 = 24
x %= 5; // x = x % 5 = 4
“`
In this example, we start with a variable x equal to 10. We then use the addition assignment operator to add 5 to x, the subtraction assignment operator to subtract 3 from x, the multiplication assignment operator to multiply x by 4, the division assignment operator to divide x by 2, and the modulus assignment operator to find the remainder when x is divided by 5. Finally, x is equal to 4.
Increment and decrement operators:
Increment and decrement operators are used in programming languages to increase or decrease the value of a variable by a fixed amount. These operators are commonly used in loops and other constructs that require iterative changes to a variable.
The increment operator is denoted by two plus signs (++) and is used to increase the value of a variable by 1. For example:
“`
int x = 5;
x++;
“`
In this code snippet, the value of `x` is initially set to 5, and then the increment operator is applied to `x`. After the increment operation, the value of `x` is 6.
The decrement operator is denoted by two minus signs (–) and is used to decrease the value of a variable by 1. For example:
“`
int y = 10;
y–;
“`
In this code snippet, the value of `y` is initially set to 10, and then the decrement operator is applied to `y`. After the decrement operation, the value of `y` is 9.
Increment and decrement operators can also be used in expressions. For example:
“`
int i = 5;
int j = ++i;
“`
In this code snippet, the value of `i` is initially set to 5, and then the prefix increment operator is applied to `i`. This increases the value of `i` to 6 and returns the new value. The value of `j` is then set to the new value of `i`, which is 6.
Similarly, the postfix increment operator can be used as follows:
“`
int i = 5;
int j = i++;
“`
In this code snippet, the value of `i` is initially set to 5, and then the postfix increment operator is applied to `i`. This returns the current value of `i`, which is 5, and then increases the value of `i` to 6. The value of `j` is set to the original value of `i`, which is 5.
It is important to note that the use of increment and decrement operators can lead to unintended consequences if not used properly. For example, consider the following code:
“`
int i = 5;
int j = i++ + ++i;
“`
In this code snippet, the value of `i` is initially set to 5. The expression `i++` returns the current value of `i`, which is 5, and then increments the value of `i` to 6. The expression `++i` increments the value of `i` to 7 and then returns the new value. The final value of `j` is therefore 5 + 7, or 12.
To avoid such issues, it is important to use increment and decrement operators carefully and to understand their behavior in different contexts.
Comparison operators:
Comparison operators are used in programming languages to compare two values and evaluate whether they are equal, greater than, less than, or not equal to each other. These operators return a Boolean value of either true or false, depending on the outcome of the comparison. Here are the most common comparison operators:
1. Equal to (==): The equal to operator compares two values to see if they are equal. For example, 4 == 4 returns true because both values are equal. However, 4 == 5 returns false because the values are not equal.
2. Not equal to (!=): The not equal to operator compares two values to see if they are not equal. For example, 4 != 5 returns true because the values are not equal. However, 4 != 4 returns false because the values are equal.
3. Greater than (>): The greater than operator compares two values to see if the first value is greater than the second value. For example, 5 > 4 returns true because 5 is greater than 4. However, 4 > 5 returns false because 4 is not greater than 5.
4. Less than (<): The less than operator compares two values to see if the first value is less than the second value. For example, 4 < 5 returns true because 4 is less than 5. However, 5 < 4 returns false because 5 is not less than 4.
5. Greater than or equal to (>=): The greater than or equal to operator compares two values to see if the first value is greater than or equal to the second value. For example, 5 >= 4 returns true because 5 is greater than 4. Also, 5 >= 5 returns true because 5 is equal to 5. However, 4 >= 5 returns false because 4 is not greater than or equal to 5.
6. Less than or equal to (<=): The less than or equal to operator compares two values to see if the first value is less than or equal to the second value. For example, 4 <= 5 returns true because 4 is less than 5. Also, 5 <= 5 returns true because 5 is equal to 5. However, 5 <= 4 returns false because 5 is not less than or equal to 4.
Here is an example code snippet that demonstrates the use of comparison operators in Python:
“`python
# Comparison operators example
x = 5
y = 10
# Equal to operator
print(x == y) # Output: False
# Not equal to operator
print(x != y) # Output: True
# Greater than operator
print(x > y) # Output: False
# Less than operator
print(x < y) # Output: True
# Greater than or equal to operator
print(x >= y) # Output: False
# Less than or equal to operator
print(x <= y) # Output: True
“`
In this example, we assign values to the variables x and y and then use the comparison operators to compare the values of x and y. The output of each comparison is printed to the console, along with a Boolean value indicating whether the comparison is true or false.
Logical operators:
Logical operators are used to combining one or more boolean expressions in order to form a more complex boolean expression. In programming, logical operators are used to making decisions based on multiple conditions. There are three basic logical operators: AND, OR, and NOT.
1. AND Operator: The AND operator returns true only if both operands are true. Otherwise, it returns false. The symbol used for the AND operator is ‘&&’ in most programming languages. Here’s an example:
“`
int a = 5, b = 10;
if (a > 0 && b > 0) {
// Both a and b are positive.
}
“`
In the above example, the condition inside the if statement will only be true if both a and b are positive.
2. OR Operator: The OR operator returns true if at least one of the operands is true. Otherwise, it returns false. The symbol used for the OR operator is ‘||’ in most programming languages. Here’s an example:
“`
int a = 5, b = -10;
if (a > 0 || b > 0) {
// At least one of a and b is positive.
}
“`
In the above example, the condition inside the if statement will be true because a is positive.
3. NOT Operator: The NOT operator is a unary operator that negates the value of its operand. If the operand is true, the NOT operator returns false. If the operand is false, the NOT operator returns true. The symbol used for the NOT operator is ‘!’ in most programming languages. Here’s an example:
“`
bool a = true;
if (!a) {
// The value of a is false.
}
“`
In the above example, the condition inside the if statement will be true because the value of ‘a’ is negated to false using the NOT operator.
Logical operators are commonly used in conditional statements to check multiple conditions at once. By using logical operators, programmers can create more complex conditions that can help make their programs more robust and efficient.
Bitwise operators:
Bitwise operators are operators that operate on binary values, meaning values that are represented in binary format using 0s and 1s. These operators are used to manipulate individual bits in binary numbers. There are six bitwise operators in the Python language:
1. Bitwise AND (&): This operator returns a binary value where each bit is set to 1 if both corresponding bits in the operands are 1, otherwise it sets the bit to 0. For example, if we have two binary numbers 1100 and 1010, the result of their bitwise AND would be 1000.
Example:
“`
a = 0b1100 # binary representation of 12
b = 0b1010 # binary representation of 10
c = a & b # bitwise AND of a and b
print(bin(c)) # prints ‘0b1000’
“`
2. Bitwise OR (|): This operator returns a binary value where each bit is set to 1 if at least one corresponding bit in the operands is 1, otherwise it sets the bit to 0. For example, if we have two binary numbers 1100 and 1010, the result of their bitwise OR would be 1110.
Example:
“`
a = 0b1100 # binary representation of 12
b = 0b1010 # binary representation of 10
c = a | b # bitwise OR of a and b
print(bin(c)) # prints ‘0b1110’
“`
3. Bitwise XOR (^): This operator returns a binary value where each bit is set to 1 if only one corresponding bit in the operands is 1, otherwise it sets the bit to 0. For example, if we have two binary numbers 1100 and 1010, the result of their bitwise XOR would be 0110.
Example:
“`
a = 0b1100 # binary representation of 12
b = 0b1010 # binary representation of 10
c = a ^ b # bitwise XOR of a and b
print(bin(c)) # prints ‘0b0110’
“`
4. Bitwise NOT (~): This operator is a unary operator that returns the complement of a binary value. It flips each bit in the operand. For example, if we have a binary number 1100, the result of bitwise NOT would be 0011.
Example:
“`
a = 0b1100 # binary representation of 12
b = ~a # bitwise NOT of a
print(bin(b)) # prints ‘-0b1101’ (note that a result is a negative number)
“`
5. Left shift (<<): This operator shifts the bits of the left operand to the left by a number of positions specified by the right operand. The vacant bits are filled with 0’s. For example, if we have a binary number 1100 and we left shift it by 2 positions, the result would be 110000.
Example:
“`
a = 0b1100 # binary representation of 12
b = a << 2 # left shift of a by 2 positions
print(bin(b)) # prints ‘0b110000’
“`
6. Right shift (>>): This operator shifts the bits of the left operand to the right by a number of positions specified by the right operand. The vacant bits are filled with 0’s if the left operand is non-negative, and with 1’s if the left operand is negative. For example, if we have a binary number 1100 and we right-shift it by 2 positions
Ternary operator:
The ternary operator is a shorthand way of writing a simple if-else statement in a single line of code. It’s often used when you want to assign a value to a variable based on a condition. The ternary operator has the following syntax:
“`
condition ? value_if_true : value_if_false;
“`
The `condition` is an expression that evaluates to either true or false. If the condition is true, the operator returns `value_if_true`, otherwise it returns `value_if_false`.
Here’s an example that uses the ternary operator to check if a number is even or odd:
“`
int num = 7;
string result = num % 2 == 0 ? “even” : “odd”;
“`
In this example, we first declare a variable `num` and assign it the value 7. We then use the ternary operator to check if `num` is even or odd. The condition is `num % 2 == 0`, which checks if `num` is divisible by 2 (i.e., even). If the condition is true, the operator returns the string `”even”`. Otherwise, it returns the string `”odd”`.
The resulting value is then assigned to the variable `result`. The final value of the `result` is `”odd”`, since 7 is an odd number.
We could achieve the same result using an if-else statement like this:
“`
int num = 7;
string result;
if (num % 2 == 0) {
result = “even”;
} else {
result = “odd”;
}
“`
But the ternary operator allows us to write the same code in a shorter and more concise way.
It’s worth noting that the ternary operator should only be used for simple if-else statements. If the conditions become more complex, it’s generally better to use an if-else statement for better readability and maintainability.
Also, read Lattice and Recurrence Relation