Categories
a. What are Operators ?

C Arithmetic Operators

The C language supports all the basic arithmetic operators such as additionsubtractionmultiplicationdivision, etc.

The following table shows all the basic arithmetic operators along with their descriptions.

OperatorDescriptionExample(where a and b are variables with some integer value)
+adds two operands (values)a+b
-subtract second operands from firsta-b
*multiply two operandsa*b
/divide numerator by the denominator, i.e. divide the operand on the left side with the operand on the right sidea/b
%This is the modulus operator, it returns the remainder of the division of two operands as the resulta%b
++This is the Increment operator – increases integer value by one. This operator needs only a single operand.a++ or ++a
--This is the Decrement operator – decreases integer value by one. This operator needs only a single operand.--b or b--

Example: Basic Arithmetic Operators

Let’s see a code example to understand the use of the basic arithmetic operators in C programs.

#include <stdio.h>

int main() {

    int a = 50, b = 23, result;

    // addition
    result = a+b;
    printf("Addition of a & b = %d \n",result);

    // subtraction
    result = a-b;
    printf("Subtraction of a & b = %d \n",result);

    // multiplication
    result = a*b;
    printf("Multiplication of a & b = %d \n",result);

    // division
    result = a/b;
    printf("Division of a & b = %d \n",result);

    return 0;

}

Output

Addition of a & b = 73, Subtraction of a & b = 27, Multiplication of a & b = 1150, Division of a & b = 2,

Example: Using Modulus Operator (%)

The modulus operator returns the remainder value after the division of the provided values.

#include <stdio.h>

int main() {

   int a = 23, b = 20, result;

   // Using Modulus operator
   result = a%b;

   printf("result = %d",result);

   return 0;

}

Output

result = 3

Example: Using Increment and Decrement Operators

The increment operator is used to increase the value of any numeric value by 1, whereas the decrement operator is used to decrease the value of any numeric value by 1.

#include <stdio.h>

int main() {

   int a = 10, b = 20, c, d;

   /* 
      Using increment operator
   */
   printf("Incrementing value of a = %d \n", ++a);

   /* 
      Using decrement operator
   */
   printf("Decrementing value of b = %d \n", --b);

   // first print value of a, then decrement a
   printf("Decrementing value of a = %d \n", a--);
   printf("Value of a = %d \n", a);

   // first print value of b, then increment b
   printf("Incrementing value of b = %d \n", b++);
   printf("Value of b = %d \n", b);

   return 0;

}

Output

Incrementing value of a = 11, Decrementing value of b = 19,Decrementing value of a = 11, Value of a = 10, Incrementing value of b = 19, Value of b = 20

In the code example above, we have used the increment operator as ++a and b++, while the decrement operator as --b and a--.

When we use the increment and decrement operator as a prefix (means before the operand), then first the increment operation is done and that value is used, like in the first two printf() functions, we get the updated values of a and b.

Whereas when we use the increment and decrement operators as postfix (means after the operand), then first the larger expression is evaluated which is printf() in this case and then the value of the operand is updated.

Leave a Reply

Your email address will not be published. Required fields are marked *