0% found this document useful (0 votes)
2 views

2.2 operators

The document provides an overview of various operators in C programming, including arithmetic, relational, logical, bitwise, and assignment operators. It explains their functions, examples, and precedence in expressions. The content is structured with code snippets to illustrate the usage of each operator.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

2.2 operators

The document provides an overview of various operators in C programming, including arithmetic, relational, logical, bitwise, and assignment operators. It explains their functions, examples, and precedence in expressions. The content is structured with code snippets to illustrate the usage of each operator.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

All About

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

➢ Arithmetic operators have higher priority over relational


operators.
3. Logical operators

C has the following three logical operators


➢ && meaning logical AND ( binary operator )
➢ || meaning logical OR ( binary operator )
➢! meaning logical NOT( unary operator )

Expressions connected by logical operators are


evaluated left to right, and evaluation stops as
soon as the truth or falsehood of the result is
known.
3. Logical operators
❖ The logical operators && and|| are used when we want to test
more than one condition and make decisions.
An example is
a>b && x==10

→An expression of this kind, which combines two or more


relations expressions, is termed as a logical expression or
compound relational expression.
When the above expression will true or false??????
→The logical expression given above is TRUE only if a>b is true and
x==10 is true. If either (or both) of them are false, the expression
is false.
3. Logical operators

▪ The precedence of && is higher than that of ||, and


both are lower than relational operators, so

3 < 5 && -3 < -5 || 3 > 2


True && False|| True
False|| True
True
#include <stdio.h>
Logical Operators main()
{
Operator Description Example int a = 0, b = 20;
if ( a && b )
&& Called Logical AND operator. If (A && B) is {
(A holds 1 both the operands are non- false. printf("Line 1 - Condition is true\n" );
and B holds zero, then the condition
}
0, then:) becomes true.
else
{
Called Logical OR Operator. If (A || B) is printf("Line 1 - Condition is not true\n" );
any of the two operands is true. }
|| non-zero, then the condition
becomes true.
if ( a || b )
{
printf("Line 2 - Condition is true\n" );
}
Called Logical NOT Operator. !(A && B) is
It is used to reverse the true.
else
! logical state of its operand. If {
a condition is true, then printf("Line 2 - Condition is not true\n" );
Logical NOT operator will }
make it false.
if ( !(a && b) )
{
printf("Line 3 - Condition is true\n" );
}}
Bitwise Operators
Operator Description Example

& 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

~ Binary Ones Complement Operator is (~A ) = -61, i.e.,


unary and has the effect of 'flipping' bits.
1100 0011 in 2's
complement
form. A&B = 0000 1100
<< Binary Left Shift Operator. The left A << 2 = 240,
operands value is moved left by the i.e., 1111 0000 A|B = 0011 1101
number of bits specified by the right A^B = 0011 0001
operand.
>> Binary Right Shift Operator. The left A >> 2 = 15, i.e., ~A = 1100 0011
operands value is moved right by the 0000 1111
number of bits specified by the right
operand.
Assignment Operators
Operator Description Example
Simple assignment operator. Assigns values from right side C = A + B will assign the value of A + B to C
=
operands to left side operand.
Add AND assignment operator. It adds the right operand to the left C += A is equivalent to C = C + A
+=
operand and assigns the result to the left operand.
Subtract AND assignment operator. It subtracts the right operand C -= A is equivalent to C = C - A
-=
from the left operand and assigns the result to the left operand.

Multiply AND assignment operator. It multiplies the right operand C *= A is equivalent to C = C * A


*=
with the left operand and assigns the result to the left operand.

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.

Modulus AND assignment operator. It takes modulus using two C %= A is equivalent to C = C % A


%=
operands and assigns the result to the left operand.
Left shift AND assignment operator. C <<= 2 is same as C
<<= = C << 2
Right shift AND assignment operator. C >>= 2 is same as C
>>= = C >> 2
Bitwise AND assignment operator. C &= 2 is same as C= C & 2
&=
^= Bitwise exclusive OR and assignment C ^= 2 is same as C
operator. =C^2

|= Bitwise inclusive OR and assignment C |= 2 is same as C = C | 2


operator.

You might also like