Explain different arithmetical and logical operators available in C, with the help of examples.
Explain different arithmetical and logical operators available in C, with the help of examples.?
Operator Basics
Operators in C language are symbols that tell the compiler to perform some mathematical calculations or logical operations (we will look at in a while) on the variables and constants. For example, consider the mathematical operation "10 + 2 - a"
. Here '+' and '-' are called operators and the values on which operators work ('10', '2', and 'a') are known as operands. The c programming language has a wide variety of operators which are categorized into different groups based on the type of operations they perform.
Types of Operators
Type of Operator | Description |
---|---|
Arithmetic operator | Arithmetic operators are used to performing mathematical operations. |
Relational operator | Relational operators are used to performing comparison operations. |
Logical operator | Logical operators are used to performing the basic operation of boolean algebra ('AND', 'OR', and 'NOT'). |
Bitwise operator | Bitwise operators are used to performing bitwise manipulation on operands. |
Assignment operator | The assignment operator is used to assign the value of the second operand to the first. |
Conditional operator | Conditional operators operate on a condition and return a result based on the truth value of the condition. |
Now let's take a look at different types of operators and it examples in detail.
Arithmetic Operators
Arithmetic operators are used to performing basic mathematical operations like addition, subtraction, multiplication, and division. There are five arithmetic operators available in C (+, -, *, /, %
). All arithmetic operators are binary operators, ie; they operate on two operands to produce the result.
The table below lists the arithmetic operators
Operator | Name | Description | Example |
---|---|---|---|
+ | Addition Operator | '+' Operator adds two values. This operator works on integer, float, and character variables. |
|
- | Subtraction Operator | '-' Operator produces a result after subtracting two values. It also works on integer, float, character variables. |
|
* | Multiplication Operator | '*' Operator produces results after the multiplication of operands. |
|
/ | Division Operator | '/' Operator produces a result of the division of the first operand by second. |
|
& | Modulus Operator | '%' Operator generates the remainder after integer division. |
|
Examples of Arithmetic Operators
- #include <stdio.h>
- int main()
- {
- int a = 25, b = 10;
- printf("Sum: %d\n", a + b);
- printf("Difference: %d\n", a - b);
- printf("Product: %d\n", a * b);
- printf("Quotient: %d\n", a / b);
- printf("Remainder: %d\n", a % b);
- return 0;
- }
- Output:
- Sum: 35
- Difference: 15
- Product: 250
- Quotient: 2
- Remainder: 5
Increment and Decrement operators ( ++, --)
There are two special arithmetic operators called increment and decrement operators available C. They are unary operators, unlike all other arithmetic operators. The operators are denoted as : '++' (increment operator), '--' (decrement operator).
Operator | Name | Description | Example |
---|---|---|---|
++ | Increment Operator | Increments the value of operand by 1 | a++ : This is equivalent to a = a + 1 |
-- | Decrement Operator | Decrements the value of operand by 1 | a-- : This is equivalent to a = a - 1 |
Variants of Increment and Decrement Operators - Pre Increment, Post Increment, Pre Decrement, and Post Decrement
- Pre Increment Operators: When '++' is used as a prefix of the operand, it's called a pre-increment operator. The pre-increment operator will increment the value of the operand before using it in the expression.
- Post Increment Operators: When '++' is used as a postfix of the operand, it's called post-increment operator. Post increment operator will increment the value of the operand after using it in the expression. ie; The current expression will use the non-incremented value of the operand.
- Pre Decrement Operators: When '--' is used as a prefix of the operand, it's called pre decrement operator. The pre decrement operator will decrement the value of the operand before using it in the expression.
- Post Decrement Operators: When '--' is used as a postfix of the operand, it's called post decrement operator. Post decrement operator will decrement the value of the operand after using it in the expression. ie; The current expression will use the non-decrement value of the operand.
There are two different variants of increment and decrement operators known as pre-increment, post-increment, pre decrement, and post decrement operators.
Examples of pre increment, post increment, pre decrement and post decrement operators are given below
- #include <stdio.h>
- int main(void) {
- int a = 5;
- // Pre increment operator. Value of 'a' will be incremented first and then
- // used in the expression. So b is assigned value after a gets incremented.
- int b = ++a;
- printf("a = %d, b = %d\n", a, b);
- a = 5;
- // Post increment operator. The value of 'a' will be incremented only after
- // it gets used in the expression. That means, first b will get assigned the
- // value of 'a' and then value of 'a' will be incremented .
- b = a++;
- printf("a = %d, b = %d\n", a, b);
- a = 5;
- // Pre decrement operator. Similar to pre increment operator, pre decrement
- // operator decrements the value of operand before using it in the expression.
- b = --a;
- printf("a = %d, b = %d\n", a, b);
- a = 5;
- // Post decrement operator. Similar to post increment operator, post decrement
- // operator decrements the value of operand after using it in the expression.
- b = a--;
- printf("a = %d, b = %d\n", a, b);
- return 0;
- }
- Output:
- a = 6, b = 6
- a = 6, b = 5
- a = 4, b = 4
- a = 4, b = 5
Assignment Operator
The assignment operator assigns the value of the expression on the right side to the left side variable. The base assignment operator is '='. In C, you can use this operator like the following
variable = expression
Here variable can be any kind of a variable and the expression can be a simple constant, another variable, or maybe a more complex expression, like a formula. The value of the expression will be evaluated and assigned to the variable. But, there are some things to note about the assignment operator.
If the value of the expression on the right side is not the same data type as the variable on the left, then the value of the expression will be converted to the same data type as the variable. Suppose, an expression is a floating-point number and the variable is an integer. Then, the floating-point number will be converted to an integer and then will be assigned to the variable. It is better to use the same data types on both sides unless it is a must, else it may lead to information loss.C also allows multiple assignment statement like
variable1 = variable2 = ... = variableN = expression.
This means, the value of the expression will be assigned to 'variable', which in turn will be assigned to the variable left of 'variable' and it propagates till 'variable1'. So, after the execution of multiple assignment operation, all the variables will have the same value.Apart from the base assignment operator explained above, C also has different variants of the same assignment operator. They are
'+=', '-=', '*=', '/=', '%='
. The behavior of these operators is explained below.Operator
Name
Description
Example
+=
Add and Assignment Operator
This operator performs the addition of operand on the left side with the operand right side and the result will be assigned to the operand on the left side.
a += 10
: This is equivalent toa = a + 10
-=
Subtract and Assignment Operator
This operator subtracts the operand on the right side from the operand on the left side and the result will be assigned to the left side operand.
a -= 10
: This is equivalent toa = a - 10
*=
Multiplication and Assignment Operator
This operator multiplies the operand on the right side with the operand on the left side and the result will be assigned to the left side operand.
a *= 10
: This is equivalent toa = a * 10
/=
Division and Assignment Operator
This operator divides the operand on the left side with the operand on the right side and the result will be assigned to the left side operand.
a /= 10
: This is equivalent toa = a / 10
%=
Modulus and Assignment Operator
This operator computes the modulus of the left side operand by the right-side operand and the result will be assigned to the left side operand.
a %= 10
: This is equivalent toa = a % 10
Examples of Assignment Operator
- #include <stdio.h>
- int main()
- {
- int a = 25, b = 10;
- a += b;
- printf("'a' after a += b: %d\n", a);
- a = 25, b = 10;
- a -= b;
- printf("'a' after a -= b: %d\n", a);
- a = 25, b = 10;
- a *= b;
- printf("'a' after a *= b: %d\n", a);
- a = 25, b = 10;
- a /= b;
- printf("'a' after a /= b: %d\n", a);
- a = 25, b = 10;
- a %= b;
- printf("'a' after a \%= b: %d\n", a);
- return 0;
- }
- Output:
- 'a' after a += b: 35
- 'a' after a -= b: 15
- 'a' after a *= b: 250
- 'a' after a /= b: 2
- 'a' after a %= b: 5
Relational operator
Relational operators are binary operators(operates on two operands) and are used to relate or compare two operands. There are four relational operators in C (i.e
<, <=, >, >=
). If the relationship between the operands is correct, it will return 1 and returns 0 otherwise.Apart from four relational operators, C has two equality operators (== and !=) as well for comparing operands. Now let's take a look at different relational and equality operators and how they operate on the operands.
Operator
Name
Description
Example
<
'Less than' operator
Checks if the first operand is less than the second operand and returns 1 if it's true, else returns 0
10 < 5
returns 0 and5 < 10
returns 1<=
'Less than or equals to' operator
Checks if the first operand is less than or equals to the second operand and returns 1 if it's true, else returns 0
10 <= 10
returns 1 and10 <= 5
returns 0>
'Greater than' operator
Checks if the first operand is greater than the second operand and returns 1 if it's true, else returns 0
10 > 5
returns 1 and5 > 10
returns 0>=
'Greater than or equals to' operator
Checks if the first operand is greater than or equals to the second operand and returns 1 if it's true, else returns 0
10 >= 10
returns 1 and5 >= 10
returns 0==
Equality operator
Checks, if the two operands are equal, are returns 1 if it's true, else returns 0
10 == 10
returns 1 and10 == 1
returns 0!=
Non-equality operator
Checks, if the two operands are equal, are returns 1 if they are not equal, else returns 0
10 != 10
returns 0 and10 != 1
returns 1Examples of Relational Operator
- #include <stdio.h>
- int main()
- {
- int a = 25, b = 10;
- printf("%d < %d : %d\n", a, b, a < b);
- printf("%d <= %d: %d\n", a, b, a <= b);
- printf("%d > %d : %d\n", a, b, a > b);
- printf("%d >= %d: %d\n", a, b, a >= b);
- printf("%d == %d: %d\n", a, b, a == b);
- printf("%d != %d: %d\n", a, b, a != b);
- return 0;
- }
- Output:
- 25 < 10 : 0
- 25 <= 10: 0
- 25 > 10 : 1
- 25 >= 10: 1
- 25 == 10: 0
- 25 != 10: 1
Logical operator
We've learned about relational operators, using which we can do comparisons. What if you need to combine two relational expressions, for example, if you want to check if a number is greater than 10 and less than 20? You can use logical operators for that purpose. There are three logical operators in C language,
&&, ||, !
. Details and examples of logical operators are given below
Examples of Logical OperatorsOperator
Name
Description
Example
&&
'Logical AND' Operator
'AND' operator returns true if both the operands it operates on evaluates to true (non-zero), else return false
a && b
returns true if both a and b are non-zero||
'Logical OR' Operator
'OR' operator returns true if any of the operands it operates on evaluates to true (non-zero), else return false
a || b
returns true if either a or b are non-zero!
'Logical NOT' Operator
'NOT' operator is a unary operator (it operates only on one operand). It returns true if the operand is false and returns false if the operand is true
returns true if a is false. Else returns false
- #include <stdio.h>
- int main(void) {
- int a = 5, b = 6;
- printf("(a == 5) && (b == 7) : %d\n", (a == 5) && (b == 7));
- printf("(a == 5) || (b == 7) : %d\n", (a == 5) || (b == 7));
- printf("!(a == 5) : %d", !(a == 5));
- return 0;
- }
- Output:
- (a == 5) && (b == 7) : 0
- (a == 5) || (b == 7) : 1
- !(a == 5) : 0
What do you think?
I hope, now you have a better understanding of operators. Comments and suggestions regarding this article are welcome.
Comments
Post a Comment