Arithmetic Operators

Arithmetic Operators

Published by: Nuru

Published date: 21 Jun 2021

Arithmetic Operators Photo

Arithmetic Operators

Arithmetic operators perform arithmetic operations. There are five arithmetic operators i.e. add, sub, divide, multiply, and power.

  • Arithmetic Operators perform arithmetic operations.
  • C provides all the basic arithmetic operators.
  • There are five arithmetic operators in C.

Arithmetic Operators

Note:-

  • The operands acted upon by arithmetic operators must be numeric values (int, float, etc.).
  • The division operator requires that the second operand be non-zero.
  • Integer division truncates any fractional part.
  • The modulo division operator (remainder operator) requires that both operands be integers and that the second operand be non-zero.
  • The unary minus operator multiplies its single operand by -1.

Integer Arithmetic, Real Arithmetic, and Mixed-mode Arithmetic

  • When both the operands in a single arithmetic expression such as a+b are integers, the expression is called an integer expression, and the operation is called integer arithmetic.
  • An arithmetic operation involving only real operands is called real arithmetic.
  • When one of the operands is real and the other is an integer, the expression is called a mixed-mode arithmetic expression.

Note:-

  • Integer arithmetic always yields an integer value.
  • The operator % cannot be used with real operands.
  • For Integer Division, when both operands are of the same sign, the result is truncated towards 0.
    E.g. 6/7=0 and -6/-7=0.
  • However, when one of the operands is negative, then the truncation is machine-dependent i.e. -6/7 maybe 0 or -1.
  • For modulo division, the sign of the result is always the sign of the first operand (the dividend).
    E.g. -14 % 3 = -2
    -14%-3 = -2
    14 % -3 = 2
  • Division Rule for Integer Arithmetic, Real Arithmetic and Mixed-mode Arithmetic
    1. int / int = int
    2. float / float = float
    3. int / float = float
    4. float / int = float

Some Examples

#include
#include
void main()
{
int months, days;
clrscr();

printf(“Enter days\n”);
scanf(“%d”, &days);
months=days/30;
days=days%30;
printf(“Months=%d Days=%d”, months, days);
getch();
}

 

#include
#include
void main()
{
int a,b,c;
float d,e,f;
clrscr();

a=8; b=3; c=4;
d=12.5; e=6.25; f=3.5;
printf(“a/b=%d \t a/c=%d”, a/b, a/c);
printf(“\nd/e=%f \t d/f=%f \t d/a=%f”, d/e, d/f, d/a);
printf(“\n Unary minus: -a = %d”, -a);
getch();
}