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 OperatorDescription

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

OperatorNameDescriptionExample

+

Addition Operator

'+' Operator adds two values. This operator works on integer, float, and character variables.

20 + 10 return 20

-

Subtraction Operator

'-' Operator produces a result after subtracting two values. It also works on integer, float, character variables.

20 - 10 returns 10

*

Multiplication Operator

'*' Operator produces results after the multiplication of operands.

20 * 10 returns 200

/

Division Operator

'/' Operator produces a result of the division of the first operand by second.

20 / 10 returns 2

&

Modulus Operator

'%' Operator generates the remainder after integer division.

25 % 10 returns 5

Examples of Arithmetic Operators

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. int a = 25, b = 10;
  6. printf("Sum: %d\n", a + b);
  7. printf("Difference: %d\n", a - b);
  8. printf("Product: %d\n", a * b);
  9. printf("Quotient: %d\n", a / b);
  10. printf("Remainder: %d\n", a % b);
  11. return 0;
  12. }
  13.  
  14. Output:
  15. Sum: 35
  16. Difference: 15
  17. Product: 250
  18. Quotient: 2
  19. 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).

OperatorNameDescriptionExample
++Increment OperatorIncrements the value of operand by 1a++ : This is equivalent to a = a + 1
--Decrement OperatorDecrements the value of operand by 1a-- : 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

    1. #include <stdio.h>
    2.  
    3. int main(void) {
    4. int a = 5;
    5. // Pre increment operator. Value of 'a' will be incremented first and then
    6. // used in the expression. So b is assigned value after a gets incremented.
    7. int b = ++a;
    8. printf("a = %d, b = %d\n", a, b);
    9. a = 5;
    10. // Post increment operator. The value of 'a' will be incremented only after
    11. // it gets used in the expression. That means, first b will get assigned the
    12. // value of 'a' and then value of 'a' will be incremented .
    13. b = a++;
    14. printf("a = %d, b = %d\n", a, b);
    15. a = 5;
    16. // Pre decrement operator. Similar to pre increment operator, pre decrement
    17. // operator decrements the value of operand before using it in the expression.
    18. b = --a;
    19. printf("a = %d, b = %d\n", a, b);
    20.  
    21. a = 5;
    22. // Post decrement operator. Similar to post increment operator, post decrement
    23. // operator decrements the value of operand after using it in the expression.
    24. b = a--;
    25. printf("a = %d, b = %d\n", a, b);
    26. return 0;
    27. }
    28.  
    29. Output:
    30. a = 6, b = 6
    31. a = 6, b = 5
    32. a = 4, b = 4
    33. 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 to a = 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 to a = 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 to a = 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 to a = 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 to a = a % 10

    Examples of Assignment Operator

    1. #include <stdio.h>
    2.  
    3. int main()
    4. {
    5. int a = 25, b = 10;
    6. a += b;
    7. printf("'a' after a += b: %d\n", a);
    8. a = 25, b = 10;
    9. a -= b;
    10. printf("'a' after a -= b: %d\n", a);
    11. a = 25, b = 10;
    12. a *= b;
    13. printf("'a' after a *= b: %d\n", a);
    14. a = 25, b = 10;
    15. a /= b;
    16. printf("'a' after a /= b: %d\n", a);
    17. a = 25, b = 10;
    18. a %= b;
    19. printf("'a' after a \%= b: %d\n", a);
    20. return 0;
    21. }
    22.  
    23. Output:
    24. 'a' after a += b: 35
    25. 'a' after a -= b: 15
    26. 'a' after a *= b: 250
    27. 'a' after a /= b: 2
    28. '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 and 5 < 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 and 10 <= 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 and 5 > 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 and 5 >= 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 and 10 == 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 and 10 != 1 returns 1

    Examples of Relational Operator

    1. #include <stdio.h>
    2.  
    3. int main()
    4. {
    5. int a = 25, b = 10;
    6. printf("%d < %d : %d\n", a, b, a < b);
    7. printf("%d <= %d: %d\n", a, b, a <= b);
    8. printf("%d > %d : %d\n", a, b, a > b);
    9. printf("%d >= %d: %d\n", a, b, a >= b);
    10. printf("%d == %d: %d\n", a, b, a == b);
    11. printf("%d != %d: %d\n", a, b, a != b);
    12. return 0;
    13. }
    14.  
    15. Output:
    16. 25 < 10 : 0
    17. 25 <= 10: 0
    18. 25 > 10 : 1
    19. 25 >= 10: 1
    20. 25 == 10: 0
    21. 25 != 10: 1
    22.  

    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

    Operator

    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

    Examples of Logical Operators

    1. #include <stdio.h>
    2.  
    3. int main(void) {
    4. int a = 5, b = 6;
    5. printf("(a == 5) && (b == 7) : %d\n", (a == 5) && (b == 7));
    6. printf("(a == 5) || (b == 7) : %d\n", (a == 5) || (b == 7));
    7. printf("!(a == 5) : %d", !(a == 5));
    8. return 0;
    9. }
    10.  
    11. Output:
    12. (a == 5) && (b == 7) : 0
    13. (a == 5) || (b == 7) : 1
    14. !(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

Popular posts from this blog

What is a string ? Write a function in C for string concatenation. Without the use of inbuilt string function?

What is the scope of a Variable? Explain the difference between a global and local variables with example programs.

What is a pointer ? Write a C program using pointer to print the name and price of the items sold in a retail shop on a specific date.