C Arithmetic Operators
C Arithmetic Operators
* multiplication
/ division
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
getch();
}
Output
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1
getch();
}
Output
++a = 11
--b = 99
++c = 11.500000
--d = 99.500000
C Assignment Operators
An assignment operator is used for assigning a value to a variable. The most
common assignment operator is =
Operator Example Same as
= a=b a=b
Operator Example Same as
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);
getch();
}
Output
c = 5
c = 10
c = 5
c = 25
c = 5
c = 0
C Relational Operators
== Equal to 5 == 3 is evaluated to 0
getch();
}
Output
5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1
C Logical Operators
Logical AND. True only if all If c = 5 and d = 2 then, expression ((c==5) &&
&&
operands are true (d>5)) equals to 0.
Logical OR. True only if either one If c = 5 and d = 2 then, expression ((c==5) ||
||
operand is true (d>5)) equals to 1.
#include <stdio.h>
#include<conio.h>
void main()
{
int a = 5, b = 5, c = 10, result;
getch();
}
Output
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0