Table of Contents
show
The list of arithmetic operators are given as,
Unary
Unary Plus
Unary plus operator appears at the left side of the operand
+
Unary Minus
Unary Minus operator appears at the left side of the operand
-
Increment
- The increment operator can appear towards the left side or towards the right side of its operand.
- If it appears towards the left side of its operand, then it is pre-increment operator (Ex:++a; (a=a+1)
- If it appears towards the right side of the operand, then it is post-increment operator Ex: a++ ; (a=a+1)
++
Decrement
- The decrement operator can appear towards the left side or towards the right side of its operand.
- If it appears towards the left side of its operand, then it is pre-decrement operator (Ex: –a; a= a-1)
- If it appears towards the right side of the operand, then it is post-decrement operator (Ex: a–; a=a-1)
--
Binary
If the operands of a binary arithmetic operator are of different but compatible types, C automatically applies arithmetic type conversion to bring the operands to a common type. This is called implicit type conversion.
Good to Know
Modulus operator can operate only on integer operands
Difference between pre-increment and post-increment
- In case of pre-increment operator, first the value of its operand is incremented and then it is used for the evaluation of expression
- In case of post-increment operator, the value of operand is used first for the evaluation of the expression and after its use, the value of the operand is incremented
Difference between pre-decrement and post-decrement
- In case of pre-decrement operator, first the value of its operand is decremented and then used for the evaluation of the expression in which it appears
- In case of post-decrement operator, first the value of operand is used for the evaluation of the expression in which it appears and then the value will be decremented.
Example
Illustration of increment operator
#include<stdio.h>
int main()
{
int a=2, b= 2, c, d;
c = ++a;
d = b++;
printf("a=%d,b=%d,c=%d,d=%d",a,b,c,d);
return 0;
}
Output
a=3,b=3,c=3,d=2
Illustration of decrement operator
#include<stdio.h>
int main()
{
int a=2, b= 2, c, d;
c = --a;
d = b--;
printf("a=%d,b=%d,c=%d,d=%d",a,b,c,d);
return 0;
}
Output
a=1,b=1,c=1,d=2
Illustration of Binary arithmetic operators
#include<stdio.h>
int main()
{
int a=2, b= 2, c, d,f;
float e;
c = a+b;
d = a-b;
e = a/3.0;
f = a%2;
printf("a=%d,b=%d,c=%d,d=%d,e=%f,f=%d",a,b,c,d,e,f);
return 0;
}
Output
a=2,b=2,c=4,d=0,e=0.666667,f=0
Views: 1