Arithmetic Operators

An arithmetic operator is a mathematical functionality that takes two operands and performs a calculation on them. They are used in common arithmetic calculation and most computer languages contain a set of such operators that can be used within equations to perform a number of types of sequential calculations.

In C language, there are five main arithmetic operators. Which are shown below:

+ Addition a+b return addition of a and b
- Subtraction a-b return the difference between a and b
* Multiplication a*b a*b return the product of a and b
/ Division a/b return division of a and b
% Modulus a%b perform division of a and b and return remainder

 

These operators can operate on any built-in datatype in C programming. There is no exponentiation operator in C however you can use library function pow( ) to carry out exponentiation.

When  an arithmetic operation is performed on two integers then such operation is called Integer arithmetic. It always gives an integer as the result.

Let us understand through a program:

#include<stdio.h>
int main( )
{
	Int a=20, b=10, cal;

	/* printing value of a and b */
	printf("\na=%d and b=%d",a,b);  

	cal= a+b;   //adding two values
	printf("\n value of cal is %d", cal); 

	cal=a*b; //multiplying two values
	printf("\nvalue of cal is %d now", cal);

	cal=a-b;  //subtract one value from another
	printf("\nvalue of cal is %d now", cal);

	b=6; //changed the value of b
	cal=a/b; //after dividing a by b
	printf("\nvalue of cal is %d now", cal);

	/* modulus sign return its remainder value */
	cal=a%b;
	printf("\nvalue of cal is %d now", cal);

	return 0;
}

 

In the integer division the fractional part truncated. Division gives the quotient, whereas modulus gives the remainder of division.

So, the output will be:

a=20 and b=10
value of cal is 30
value of cal is 200 now
value of cal is 10 now
value of cal is 3 now
value of cal is 2 now

But, when the arithmetic operation is performed on two floating point or real numbers, such operation is called floating point arithmetic.

#include<stdio.h>
int main()
{
        float a=9.3, b=7.6, cal;
        /* printing value of a and b */
        printf("\na=%f and b=%f",a,b);  

	cal= a+b;   //adding two values
	printf("\n value of cal is %f", cal); 

	cal=a*b; //multiplying two values
	printf("\nvalue of cal is %f now", cal);

	cal=a-b;  //subtract one value from another
	printf("\nvalue of cal is %f now", cal);

	b=6; //changed the value of b
	cal=a/b; //after dividing a by b
	printf("\nvalue of cal is %f now", cal);

	getch();
	return 0;
}

 

So the output will be :

a=9.300000 and b=7.600000
value of cal is 16.900000
value of cal is 70.680000 now
value of cal is 1.700000 now
value of cal is 1.550000 now

The modulus operator is not applicable for floating point numbers.So that's why we didn't use that in the above program. If we use it for floating point calculation, then the program will raise an error. Let us understand this through a program:

#include<stdio.h>
int main()
{
    float x=4.8, y=2, r;
    r=x%y;
    printf("%d", r);
    getch();
    return 0;
} 

 

The above program will raise an error

In function `main`.

`invalid operands to binary %`

Whenever we use one real operand and another integer operand and if the operation is arithmetic carried out on these two operands, then it is called mixed mode arithmetic.