2.2 operators
2.2 operators
Operators!!
#include <stdio.h>
main()
Arithmetic Operators {
int a,b,c;
printf("Enter the value of a & b\n");
scanf("%d\v%d",&a,&b);
Operator Description Example
c = a + b;
+ Adds two operands. A + B = 30 printf("Line 1 - Value of c is %d\n", c );
c = a - b;
- Subtracts second operand from the A - B = -10
printf("Line 2 - Value of c is %d\n", c );
first.
Multiplies both operands. A * B = 200 c = a * b;
* printf("Line 3 - Value of c is %d\n", c );
/ Divides numerator by de-numerator. B / A = 2 c = a / b;
printf("Line 4 - Value of c is %d\n", c );
% Modulus Operator and remainder B%A=0
c = a % b;
of after an integer division.
printf("Line 5 - Value of c is %d\n", c );
++ Increment operator increases the A++ = 11 c = a++;
integer value by one. printf("Line 6 - Value of c is %d\n", c );
c = a--;
-- Decrement operator decreases the A-- = 9
printf("Line 7 - Value of c is %d\n", c );
integer value by one.
}
#include <stdio.h> Operator Description Example
main()
{ Checks if the values of two operands are (A == B) is
Int a=10,b=20;
Relational Operators:
== equal or not. If yes, then the condition
if( a == b ) { becomes true.
not true.
printf("Line 1 - a is equal to b\n" );
Checks if the values of two operands are (A != B) is
}
else
!= equal or not. If the values are not equal,
true.
{ then the condition becomes true.
printf("Line 1 - a is not equal to b\n" );
} Checks if the value of left operand is
if ( a < b ) > (A > B) is
greater than the value of right operand. If
{ yes, then the condition becomes true.
not true.
printf("Line 2 - a is less than b\n" );
}
Checks if the value of left operand is less (A < B) is
else
{
< than the value of right operand. If yes,
true.
printf("Line 2 - a is not less than b\n" ); then the condition becomes true.
} Checks if the value of left operand is
if ( a > b ) >= (A >= B) is
greater than or equal to the value of right
{
operand. If yes, then the condition
not true.
printf("Line 3 - a is greater than b\n" );
} becomes true.
else Checks if the value of left operand is less
{ <= (A <= B) is
than or equal to the value of right operand.
printf("Line 3 - a is not greater than b\n" ); If yes, then the condition becomes true.
true.
}
}
2. Relational Operators
❑A relational expression yields a value of 1 or 0.
❑ 5 < 6 TRUE 1
❑4.5<10 TRUE 1
❑-34 + 8 > 23 - 5 0
FALSE
❑4.5<-10 FALSE 0
❑5==0 FALSE 0
& Binary AND Operator copies a bit to the (A & B) = 12, i.e., Assume A = 60 and B =
result if it exists in both operands. 0000 1100 13; in binary format,
| Binary OR Operator copies a bit if it exists (A | B) = 61, i.e., they will be as follows:
in either operand. 0011 1101
A = 0011 1100
^ Binary XOR Operator copies the bit if it is (A ^ B) = 49, i.e., B = 0000 1101
set in one operand but not both. 0011 0001
Divide AND assignment operator. It divides the left operand with C /= A is equivalent to C = C / A
/=
the right operand and assigns the result to the left operand.