Arithmetic Operators in C

Written by

Namrata Jangid

Arithmetic Operators Examples:
C offers five binary operators and two unary operators.

Unary Operators:

++ This operator increases the value of the operand by 1.
– – This operator decreases the value of the operand by 1.

The code snippet below shows how we can implement unary operators:

 
#include <stdio.h>

int  main(void) {

int  operand1 = 10;

int  operand2 = 5;

printf("Before using unary operators: \n");

printf("Value of operand1: %d \n", operand1);

printf("Value of operand2: %d \n", operand2);

operand1++; //incrementing operand1 by 1

operand2--; //decrementing operand2 by 1

printf("After using unary operators: \n");

printf("Value of operand1: %d \n", operand1);

printf("Value of operand2: %d \n", operand2);

return  0;

}

The output of the above code snippet will be:

 
Before using unary operators:

Value of operand1: 10

Value of operand2: 5

After using unary operators:

Value of operand1: 11

Value of operand2: 4

  • We have created two integer variables operand1 and operand2.
  • We increment the value of operand1 by 1.
  • We decrement the value of operand2 by 1.

Binary operators:

+ Adds two operands
Subtracts the second operand from the first operand
* Multiplies two operands
/ Divides the first operand by the second operand
% Gives the remainder left after dividing the first operand by the second operand

The code snippet below shows how we can implement binary operators:

 
#include <stdio.h>

int  main(void) {

int  operand1 = 10;

int  operand2 = 5;

int  sum = operand1 + operand2;

int  difference = operand1 - operand2;

int  product = operand1 * operand2;

int  quotient = operand1 / operand2;

int  remainder = operand1 % operand2;

printf("Value of operand1: %d \n", operand1);

printf("Value of operand2: %d \n", operand2);

printf("Results: \n");

printf("operand1 + operand2: %d \n", sum);

printf("operand1 - operand2: %d \n", difference);

printf("operand1 * operand2: %d \n", product);

printf("operand1 / operand2: %d \n", quotient);

printf("operand1 %% operand2: %d \n", remainder);

return  0;

}

The output of the above code snippet is:

 
Value of operand1: 10

Value of operand2: 5

Results:

operand1 + operand2: 15

operand1 - operand2: 5

operand1 * operand2: 50

operand1 / operand2: 2

operand1 % operand2: 0

  • We have created two integer variables operand1 and operand2.
  • We have used all binary operators i.e. +, -, *, / and %, on the two operands and we have got the expected results.

Arithmetic Operators in C